
package com.lzx.iteam.provider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.lzx.iteam.AttendanceCalendarActivity;
import com.lzx.iteam.EventsFragment;
import com.lzx.iteam.bean.*;
import com.lzx.iteam.bean.aidl.ChatMoreMsg;
import com.lzx.iteam.bean.aidl.EventReplyNewData;

import com.lzx.iteam.json.JSONArray;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static android.R.attr.data;
import static android.R.attr.filter;
import static android.R.attr.value;

public class CloudDBOperation {

    CloudContactsDBHelper helper;
    private PreferenceUtil mPreferenceUtil;

    private static String TAG = "CloudDBOperation";
    private String mUserId;

    public CloudDBOperation(Context context) {
        helper = new CloudContactsDBHelper(context);
        mPreferenceUtil = PreferenceUtil.getInstance(context);
        mUserId = mPreferenceUtil.getString(PreferenceUtil.CLIENT_USEID, "－1");

    }

    public long insertStateData(String word) {
        SQLiteDatabase db = null;
        long i = 0;
        // db.execSQL("insert into state_table (state_text) values('"+word+"');");
        try {
            db = helper.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put("state_text", word);
            i = db.insert("state_table", null, cv);

        } catch (SQLException e) {
            //: handle exception
            Log.e(TAG, "Error inserting " + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        return i;
    }

    public void deleteAllStatusData() {
        SQLiteDatabase db = null;
        try {
            db = helper.getWritableDatabase();
            db.delete("state_table", null, null);
        } catch (SQLException e) {
            //: handle exception
            Log.e(TAG, "Error inserting " + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }


    public void deleteStateData(Integer id) {
        SQLiteDatabase db = null;
        try {
            db = helper.getWritableDatabase();
            int log = db.delete("state_table", "state_id=?", new String[]{
                    id.toString()
            });
//            if (Tongxingzheng.DBG) Log.d(TAG, "deleteSuccess is:" + log);
        } catch (SQLException e) {
            //: handle exception
            Log.e(TAG, "Error delete Data: " + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }

    public void updateState(Integer id, String stateText) {
        SQLiteDatabase db = null;

        try {
            db = helper.getWritableDatabase();
            ContentValues v = new ContentValues();
            v.put("state_text", stateText);
            db.update("state_table", v, "state_id=?", new String[]{
                    id.toString()
            });
        } catch (SQLException e) {
            //: handle exception
            Log.e(TAG, "Error update: " + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }

    public int getCount() {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        int count = 0;
        try {
            db = helper.getReadableDatabase();
            cursor = db.rawQuery("select count(*) from state_table", null);
            cursor.moveToFirst();
            count = cursor.getInt(0);
//            if (Tongxingzheng.DBG) Log.d(TAG, "The count is :" + count);
        } catch (SQLException e) {
            //: handle exception
            Log.e(TAG, "Error getCount: " + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return count;
    }
    //==================cloud_group表================================//

    /**
     * 创建一个团队
     *
     * @return -1表示失败，其它表示成功
     */
    public synchronized long insertCloudGroup(CloudGroup item,String... uid ) {
        SQLiteDatabase db = null;
        long flag = 0;
        try {
            db = helper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(CloudContactsDB.CloudGroupDbo.GROUP_ID, item.groupId);
            values.put(CloudContactsDB.CloudGroupDbo.GROUP_NAME, item.groupName);
            values.put(CloudContactsDB.CloudGroupDbo.GROUP_TYPE, item.groupType);
            values.put(CloudContactsDB.CloudGroupDbo.CONTACT_ID, item.contactId);
            values.put(CloudContactsDB.CloudGroupDbo.CONTACT_COUNT, item.contactCount);
            values.put(CloudContactsDB.CloudGroupDbo.MEMBER_COUNT, item.memberCount);
            values.put(CloudContactsDB.CloudGroupDbo.UPDATE_TIME, item.updateTime);
            values.put(CloudContactsDB.CloudGroupDbo.GROUP_IMG, item.groupImg);
            values.put(CloudContactsDB.CloudGroupDbo.USER_TYPE, item.userType);
            values.put(CloudContactsDB.CloudGroupDbo.IS_NEW, item.isNew);
            values.put(CloudContactsDB.CloudGroupDbo.HAS_TAGS, item.hasTags);
            if (uid != null && !StringUtil.isEmpty(uid[0])){
                values.put(CloudContactsDB.CloudGroupDbo.CREATOR_UID, uid[0]);
            }
            flag = db.insert(CloudContactsDB.CloudGroupDbo.TABLE_NAME, null, values);
        } catch (SQLException e) {
            Log.e(TAG, "Error insertChatGroup: " + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        return flag;
    }

    public ArrayList<CloudGroup> getLocalGroupList(String uid) {
        ArrayList<CloudGroup> groupList = new ArrayList<CloudGroup>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.CloudGroupDbo.TABLE_NAME, null, "creator_uid = ? ", new String[]{uid}, null, null, null);
            while (cursor.moveToNext()) {
                CloudGroup groupItem = new CloudGroup();
                groupItem.groupId = cursor.getLong(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.GROUP_ID));
                groupItem.groupName = cursor.getString(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.GROUP_NAME));
                groupItem.groupType = cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.GROUP_TYPE));
                groupItem.contactId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.CONTACT_ID));
                groupItem.contactCount = cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.CONTACT_COUNT));
                groupItem.memberCount = cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.MEMBER_COUNT));
                groupItem.updateTime = cursor.getLong(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.UPDATE_TIME));
                groupItem.groupImg = cursor.getString(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.GROUP_IMG));
                groupItem.userType = cursor.getString(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.USER_TYPE));
                groupItem.isNew = cursor.getString(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.IS_NEW));
                groupItem.hasTags = cursor.getString(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.HAS_TAGS));
                groupList.add(groupItem);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) cursor.close();
        }
        Log.d("GetGroupList", "GetGroupList");
        return groupList;
    }

    /**
     * 查询一个group
     *
     * @return
     */
    public CloudGroup getOneLocalGroupList(String groupId) {
        CloudGroup groupItem = new CloudGroup();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.CloudGroupDbo.TABLE_NAME, null, "group_id = ?", new String[]{groupId}, null, null, null);
            if (cursor.moveToNext()) {
                groupItem.groupId = cursor.getLong(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.GROUP_ID));
                groupItem.groupName = cursor.getString(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.GROUP_NAME));
                groupItem.groupType = cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.GROUP_TYPE));
                groupItem.contactId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.CONTACT_ID));
                groupItem.contactCount = cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.CONTACT_COUNT));
                groupItem.memberCount = cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.MEMBER_COUNT));
                groupItem.userType = cursor.getString(cursor.getColumnIndex(CloudContactsDB.CloudGroupDbo.USER_TYPE));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) cursor.close();
        }
        Log.d("GetgroupItem", "groupItem");
        return groupItem;
    }

    public long updateCloudGroupItem(CloudGroup item) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.CloudGroupDbo.GROUP_NAME, item.groupName);
            values.put(CloudContactsDB.CloudGroupDbo.GROUP_TYPE, item.groupType);
            values.put(CloudContactsDB.CloudGroupDbo.CONTACT_ID, item.contactId);
            values.put(CloudContactsDB.CloudGroupDbo.CONTACT_COUNT, item.contactCount);
            values.put(CloudContactsDB.CloudGroupDbo.MEMBER_COUNT, item.memberCount);
            values.put(CloudContactsDB.CloudGroupDbo.UPDATE_TIME, item.updateTime);
            values.put(CloudContactsDB.CloudGroupDbo.GROUP_IMG, item.groupImg);
            values.put(CloudContactsDB.CloudGroupDbo.USER_TYPE, item.userType);
            values.put(CloudContactsDB.CloudGroupDbo.IS_NEW, item.isNew);
            values.put(CloudContactsDB.CloudGroupDbo.HAS_TAGS, item.hasTags);
//        values.put(CloudGroupDbo.CREATOR_UID, item.creator_uid);
            flag = db.update(CloudContactsDB.CloudGroupDbo.TABLE_NAME, values, " group_id = ? ", new String[]{item.groupId + ""});
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
        return flag;
    }

    /**
     * 更新团队名称
     *
     * @param groupID 团队id
     * @param newName 新名称
     */
    public void updateCloudGrupName(String groupID, String newName) {
        SQLiteDatabase db = null;
        ContentValues values = new ContentValues();
        int update = 0;
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.CloudGroupDbo.GROUP_NAME, newName);
            String whereClause = "group_id = ? ";
            String[] whereArgs = {groupID};
            update = db.update(CloudContactsDB.CloudGroupDbo.TABLE_NAME, values, whereClause, whereArgs);

        } catch (Exception e) {
            Log.d("updateCloudGrupName", e.toString());
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.d("updateCloudGrupName", "updateCloudGrupName   " + update);
    }


    /**
     * 删除所有
     *
     * @return true表示成功，false表示失败
     */
    public synchronized boolean deleteAllGroup() {
        SQLiteDatabase db = helper.getWritableDatabase();
        int result = 0;
        try {
            result = db.delete(CloudContactsDB.CloudGroupDbo.TABLE_NAME, null, null);
        } catch (SQLException e) {
            Log.e(TAG, " error deleteAllEvents" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.d("delete", "结果 " + (result != 0));
        return (result != 0);
    }

    /**
     * 删除一个
     *
     * @param groupId
     * @return 非0表示成功，0表示失败
     */
    public int deleteGroupByGroupId(String groupId) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int delete = 0;
        try {
            delete = db.delete(CloudContactsDB.CloudGroupDbo.TABLE_NAME, "group_id = ?", new String[]{groupId});
        } catch (SQLException e) {
            Log.e(TAG, "error deleteGroupByGroupId" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }


    //===================chatgroup表==========================//

    /**
     * 创建一个聊天组
     *
     * @return -1表示失败，其它表示成功
     */
    public synchronized long insertChatGroup(ChatGroup chatGroup) {
        SQLiteDatabase db = null;
        long flag = 0;
        try {
            db = helper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(CloudContactsDB.ChatGroupData.CHAT_GROUP_ID, chatGroup.getId());
            values.put(CloudContactsDB.ChatGroupData.CHAT_GROUP_NAME, chatGroup.getName());
            values.put(CloudContactsDB.ChatGroupData.LAST_UPDATE_TIME, chatGroup.getLastUpdateTime());
            values.put(CloudContactsDB.ChatGroupData.CHAT_GROUP_IMG, chatGroup.getImg());
            values.put(CloudContactsDB.ChatGroupData.LAST_MSG_NAME, chatGroup.getLastMsgName());
            values.put(CloudContactsDB.ChatGroupData.LAST_MSG_CONTENT, chatGroup.getLastMsgContent());
            values.put(CloudContactsDB.ChatGroupData.USER_TYPE, chatGroup.getUserType());
            //values.put(CloudContactsDB.ChatGroupData.CREATOR_NAME, chatGroup.getCreatorName());
//        values.put(CloudContactsDB.ChatGroupData.UN_REGISTERED, unRegistered);
            values.put(CloudContactsDB.ChatGroupData.MEMBER_COUNT, chatGroup.getMemberCount());
            values.put(CloudContactsDB.ChatGroupData.IS_NEW, chatGroup.getIsNew());
            values.put(CloudContactsDB.ChatGroupData.UNREAD_COUNT, chatGroup.getUnreadCount());
            values.put(CloudContactsDB.ChatGroupData.TYPE, chatGroup.getType());
            values.put(CloudContactsDB.ChatGroupData.IS_ACTIVE, chatGroup.getIsActive());
            flag = db.insert(CloudContactsDB.ChatGroupData.TABLE_NAME, null, values);
        } catch (SQLException e) {
            Log.e(TAG, "Error insertChatGroup: " + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        return flag;
    }

    /**
     * 删除一个聊天组
     *
     * @param chatGroupID 要删除的聊天组的信息
     * @return 非0表示成功，0表示失败
     */
    public synchronized boolean deleteChatGroup(String chatGroupID) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int result = db.delete(CloudContactsDB.ChatGroupData.TABLE_NAME, "chatgroup_id = ?", new String[]{chatGroupID});
        db.close();
        return (result != 0);
    }

    /**
     * 删除所有聊天组
     *
     * @return 非0表示成功，0表示失败
     */
    public synchronized boolean deleteAllChatGroup() {
        SQLiteDatabase db = helper.getWritableDatabase();
        int result = db.delete(CloudContactsDB.ChatGroupData.TABLE_NAME, null, null);
        db.close();
        return (result != 0);
    }

    /**
     * 更改一个聊天的的最后更新时间(因为界面上要以最后更新时间最为排序条件，最后更新的排在最前)
     *
     * @param chatGroupID
     * @param lastUpdateTime
     */
    public synchronized void updateLastTimeOfChatGroup(String chatGroupID, String lastUpdateTime) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.ChatGroupData.LAST_UPDATE_TIME, lastUpdateTime);
        String whereClause = "chatgroup_id = ?";
        String[] whereArgs = {chatGroupID};
        db.update(CloudContactsDB.ChatGroupData.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
    }

    /**
     * 更新聊天组名称 或者 成员人数
     *
     * @param chatGroupID 聊天组id
     * @param newName     新名称
     */
    public void updateChatGrupNameOrMembers(String chatGroupID, String newName, String chatMembers) {
        SQLiteDatabase db = null;
        ContentValues values = new ContentValues();
        int update = 0;
        try {
            db = helper.getWritableDatabase();
            String whereClause = "chatgroup_id = ? ";
            String[] whereArgs = {chatGroupID};
            if (!StringUtil.isEmpty(newName)) {
                values.put(CloudContactsDB.ChatGroupData.CHAT_GROUP_NAME, newName);
            } else {
                values.put(CloudContactsDB.ChatGroupData.MEMBER_COUNT, chatMembers);
            }
            update = db.update(CloudContactsDB.ChatGroupData.TABLE_NAME, values, whereClause, whereArgs);
        } catch (Exception e) {
            Log.d("updateChatGrupName", e.toString());
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.d("updateChatGrupName", "updateChatGrupName   " + update);
    }

    /**
     * 是否有这条聊天
     *
     * @param chatGroupId
     * @return
     */
    public synchronized int getChatGroupCount(String chatGroupId) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        int count = 0;
        try {
            db = helper.getReadableDatabase();
            String[] whereArgs = {chatGroupId};
            cursor = db.rawQuery("select count(*) from chatgroup_table where chatgroup_id = ?", whereArgs);
            cursor.moveToFirst();
            count = cursor.getInt(0);
        } catch (SQLException e) {
            //: handle exception
            Log.e(TAG, "Error getCount: " + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return count;
    }

    /**
     * 更新聊天组未读消息数
     *
     * @param chatGroupID 聊天组id
     * @param unReadCount 未读消息数
     */
    public synchronized void updateChatUnReadCount(String chatGroupID, String unReadCount) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.ChatGroupData.UNREAD_COUNT, unReadCount);
        String whereClause = "chatgroup_id = ? ";
        String[] whereArgs = {chatGroupID};
        db.update(CloudContactsDB.ChatGroupData.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
    }

    /**
     * 查询所有的聊天组(以最后更新时间排序)
     *
     * @return
     */
    public synchronized ArrayList<ChatGroup> findAllChatGroup() {
        ArrayList<ChatGroup> infos = new ArrayList<ChatGroup>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            //  cursor = db.query(CloudContactsDB.ChatGroupData.TABLE_NAME, null, null, null, null, null, null);
            cursor = db.query(CloudContactsDB.ChatGroupData.TABLE_NAME, null, null, null, null, null, CloudContactsDB.ChatGroupData.LAST_UPDATE_TIME + " desc");
            while (cursor.moveToNext()) {
                ChatGroup chatGroup = new ChatGroup();
                chatGroup.id = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.CHAT_GROUP_ID));
                chatGroup.name = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.CHAT_GROUP_NAME));
                chatGroup.lastUpdateTime = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.LAST_UPDATE_TIME));
                chatGroup.img = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.CHAT_GROUP_IMG));
                chatGroup.lastMsgName = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.LAST_MSG_NAME));
                chatGroup.lastMsgContent = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.LAST_MSG_CONTENT));
                chatGroup.userType = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.USER_TYPE));
//            String creatorName = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.CREATOR_NAME));
                chatGroup.isNew = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.IS_NEW));
                chatGroup.unreadCount = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.UNREAD_COUNT));
                chatGroup.memberCount = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.MEMBER_COUNT));
                chatGroup.type = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.TYPE));
                chatGroup.isActive = cursor.getString(cursor.getColumnIndex(CloudContactsDB.ChatGroupData.IS_ACTIVE));
                infos.add(chatGroup);
            }
        } catch (SQLException e) {
            Log.e(TAG, "error findAllChatGroup " + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        //db.execSQL("SELECT * FROM chatgroup_table ORDER BY datetime(lastupdate_time) DESC");
        Log.d("GetChatList", "GetChatList");
        return infos;
    }


    /**
     * 更新最后一条聊天
     */
    public synchronized void updateChatMessage(ChatMoreMsg chatMsg, String chatGroupId) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.ChatGroupData.LAST_MSG_NAME, chatMsg.getSenderName());
        if (chatMsg.getMsgType() == Constants.MT_MSG_PIC) {
            values.put(CloudContactsDB.ChatGroupData.LAST_MSG_CONTENT, "[图片]");

        } else if (chatMsg.getMsgType() == Constants.MT_MSG_VOC) {
            values.put(CloudContactsDB.ChatGroupData.LAST_MSG_CONTENT, "[语音]");

        } else {
            if (StringUtil.isEmpty(chatMsg.getMsgContent())) {
                values.put(CloudContactsDB.ChatGroupData.LAST_MSG_CONTENT, chatMsg.getTextContent());

            } else {
                values.put(CloudContactsDB.ChatGroupData.LAST_MSG_CONTENT, chatMsg.getMsgContent());

            }

        }
        values.put(CloudContactsDB.ChatGroupData.LAST_UPDATE_TIME, chatMsg.getSendTime());
        String whereClause = "chatgroup_id = ? ";
        String[] whereArgs = {chatGroupId};
        db.update(CloudContactsDB.ChatGroupData.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
    }


    //-----------------event_table-------------------根据时间排序的存入数据库，根据人排序的未存入数据库（后期修改）

    /**
     * 添加一个事件
     *
     * @return -1表示失败，其它表示成功
     */
    public synchronized long insertEvent(EventListData eventsListData) {
        SQLiteDatabase db = null;
        long flag = -1;
        try {
            if (getEventCount(eventsListData.getEventId()) == 0) {
                db = helper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(CloudContactsDB.EventData.EVENT_ID, eventsListData.getEventId());
                values.put(CloudContactsDB.EventData.UPDATE_TIME, eventsListData.getUpdateTime());
                values.put(CloudContactsDB.EventData.EVENT_TYPE, eventsListData.getEventType());
                values.put(CloudContactsDB.EventData.EVENT_NAME, eventsListData.getEventName());
                values.put(CloudContactsDB.EventData.USER_TYPE, eventsListData.getUserType());
                values.put(CloudContactsDB.EventData.HAS_READ, eventsListData.getHasRead());
                values.put(CloudContactsDB.EventData.CREATE_TIME, eventsListData.getCreateTime());
                values.put(CloudContactsDB.EventData.USER_ID, eventsListData.getUserId());
                values.put(CloudContactsDB.EventData.USER_NAME, eventsListData.getUserName());
                values.put(CloudContactsDB.EventData.MEMBER_COUNT, eventsListData.getMemberCount());
                values.put(CloudContactsDB.EventData.EVENT_TITLE, eventsListData.getEventTitle());
                values.put(CloudContactsDB.EventData.CONTENT1, eventsListData.getContent1());
                values.put(CloudContactsDB.EventData.CONTENT2, eventsListData.getContent2());
                values.put(CloudContactsDB.EventData.CONTENT3, eventsListData.getContent3());
                values.put(CloudContactsDB.EventData.INIT_TITLE1, eventsListData.getInitTitle1());
                values.put(CloudContactsDB.EventData.INIT_TITLE2, eventsListData.getInitTitle2());
                values.put(CloudContactsDB.EventData.INIT_TITLE3, eventsListData.getInitTitle3());
                values.put(CloudContactsDB.EventData.URL1, eventsListData.getUrl1());
                values.put(CloudContactsDB.EventData.URL2, eventsListData.getUrl2());
                values.put(CloudContactsDB.EventData.URL3, eventsListData.getUrl3());
                values.put(CloudContactsDB.EventData.INIT_TITLE2, eventsListData.getInitTitle2());
                values.put(CloudContactsDB.EventData.INIT_TITLE3, eventsListData.getInitTitle3());
                values.put(CloudContactsDB.EventData.HAS_READ_COUNT, eventsListData.getHasReadCount());
                values.put(CloudContactsDB.EventData.REPLY_COUNT, eventsListData.getReplyCount());
                values.put(CloudContactsDB.EventData.REPLY_LAST_NAME, eventsListData.getReplyLastName());
                values.put(CloudContactsDB.EventData.REPLY_LAST_CONTENT, eventsListData.getReplyLastContent());
                values.put(CloudContactsDB.EventData.ATTACHMENT_THUMB_IMAGE, eventsListData.getThumbImage());
                values.put(CloudContactsDB.EventData.ATTACHMENT_ORI_IMAGE, eventsListData.getOriImage());
                values.put(CloudContactsDB.EventData.UNREAD_COUNT, eventsListData.getUnReadCount());
                values.put(CloudContactsDB.EventData.AUTHOR, eventsListData.getAuthor());
                values.put(CloudContactsDB.EventData.EVENT_AVATAR, eventsListData.getEventAvatar());
                values.put(CloudContactsDB.EventData.RECEIVER, eventsListData.getReceiver());
                values.put(CloudContactsDB.EventData.ACTIVITY_TIME, eventsListData.getActivityTime());
                values.put(CloudContactsDB.EventData.ACTIVITY_SITE, eventsListData.getActivitySite());
                values.put(CloudContactsDB.EventData.JOIN_COUNT, eventsListData.getJoinCount());
                values.put(CloudContactsDB.EventData.HAS_JOIN, eventsListData.getHasJoin());
                values.put(CloudContactsDB.EventData.VOTES, eventsListData.getVotes());
                values.put(CloudContactsDB.EventData.VOTE_NUMBER, eventsListData.getVoteMaxNumber());
                values.put(CloudContactsDB.EventData.END_TIME, eventsListData.getEndTime());
                values.put(CloudContactsDB.EventData.IS_QRCODE, eventsListData.getIsQrcode());
                values.put(CloudContactsDB.EventData.QRCODE, eventsListData.getQrcode());
                values.put(CloudContactsDB.EventData.MY_USERID, mUserId);
                flag = db.insert(CloudContactsDB.EventData.TABLE_NAME, null, values);
            }
        } catch (SQLException e) {
            Log.e(TAG, "error insertEvent" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        EventsFragment.isUpDate = true;
        return flag;
    }

    /**
     * 是否有这条事件
     *
     * @param eventId
     * @return
     */
    public synchronized int getEventCount(String eventId) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        int count = 0;
        try {
            db = helper.getReadableDatabase();
            String[] whereArgs = {eventId};
            cursor = db.rawQuery("select count(*) from event_table where event_id = ?", whereArgs);
            cursor.moveToFirst();
            count = cursor.getInt(0);
        } catch (SQLException e) {
            //: handle exception
            Log.e(TAG, "Error getCount: " + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return count;
    }

    /**
     * 分页查找所有事件
     *
     * @return
     */
    public synchronized ArrayList<EventListData> findEventsByType(String type, int currentPage, int pageSize) {
        ArrayList<EventListData> infos = new ArrayList<EventListData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        //db.beginTransaction();
        int start = (currentPage - 1) * pageSize;
        int end = start + pageSize;
        try {
            db = helper.getReadableDatabase();
            if ("0".equals(type)) {
                cursor = db.query(CloudContactsDB.EventData.TABLE_NAME, null, "my_userid = ? ", new String[]{mUserId}, null, null, CloudContactsDB.EventData.UPDATE_TIME + " desc", start + "," + end);
            } else {
                cursor = db.query(CloudContactsDB.EventData.TABLE_NAME, null, "event_type = ? and my_userid= ?", new String[]{type, mUserId}, null, null, CloudContactsDB.EventData.UPDATE_TIME + " desc", start + "," + end);
            }
            while (cursor.moveToNext()) {
                EventListData eventsListData = new EventListData();
                eventsListData.setEventId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_ID)));
                eventsListData.setUpdateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.UPDATE_TIME)));
                eventsListData.setEventType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_TYPE)));
                eventsListData.setEventName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_NAME)));
                eventsListData.setUserType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_TYPE)));
                eventsListData.setHasRead(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.HAS_READ)));
                eventsListData.setCreateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CREATE_TIME)));
                eventsListData.setUserId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_ID)));
                eventsListData.setUserName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_NAME)));
                eventsListData.setMemberCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.MEMBER_COUNT)));
                eventsListData.setEventTitle(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_TITLE)));
                eventsListData.setContent1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT1)));
                eventsListData.setContent2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT2)));
                eventsListData.setContent3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT3)));
                eventsListData.setInitTitle1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE1)));
                eventsListData.setInitTitle2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE2)));
                eventsListData.setInitTitle3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE3)));
                eventsListData.setUrl1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL1)));
                eventsListData.setUrl2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL2)));
                eventsListData.setUrl3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL3)));
                eventsListData.setReplyCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.REPLY_COUNT)));
                eventsListData.setReplyLastName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.REPLY_LAST_NAME)));
                eventsListData.setReplyLastContent(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.REPLY_LAST_CONTENT)));
                eventsListData.setThumbImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_THUMB_IMAGE)));
                eventsListData.setOriImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_ORI_IMAGE)));
                eventsListData.setUnReadCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.UNREAD_COUNT)));
                eventsListData.setAuthor(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.AUTHOR)));
                eventsListData.setEventAvatar(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_AVATAR)));
                eventsListData.setReceiver(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.RECEIVER)));
                eventsListData.setActivityTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ACTIVITY_TIME)));
                eventsListData.setActivitySite(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ACTIVITY_SITE)));
                eventsListData.setJoinCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.JOIN_COUNT)));
                eventsListData.setHasJoin(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.HAS_JOIN)));
                eventsListData.setVotes(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.VOTES)));
                eventsListData.setQrcode(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.QRCODE)));
                infos.add(eventsListData);
            }
            //db.setTransactionSuccessful();
        } catch (Exception e) {
            Log.e(TAG, " error  findEvents" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                //db.endTransaction();
                db.close();
            }
        }


        Log.i("EventList", "--所有size--" + infos.size());
        return infos;
    }

    /**
     * 分页查找所有事件
     *
     * @return
     */
    public synchronized ArrayList<EventListData> findEventsByType(String type, int pageSize) {
        ArrayList<EventListData> infos = new ArrayList<EventListData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        //db.beginTransaction();
        int start = 0;
        int end = pageSize;
        try {
            db = helper.getReadableDatabase();
            if ("0".equals(type)) {
                cursor = db.query(CloudContactsDB.EventData.TABLE_NAME, null, null, null, null, null, CloudContactsDB.EventData.UPDATE_TIME + " desc", start + "," + end);
            } else {
                cursor = db.query(CloudContactsDB.EventData.TABLE_NAME, null, "event_type = ? ", new String[]{type}, null, null, CloudContactsDB.EventData.UPDATE_TIME + " desc", start + "," + end);
            }
            while (cursor.moveToNext()) {
                EventListData eventsListData = new EventListData();
                eventsListData.setEventId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_ID)));
                eventsListData.setUpdateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.UPDATE_TIME)));
                eventsListData.setEventType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_TYPE)));
                eventsListData.setEventName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_NAME)));
                eventsListData.setUserType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_TYPE)));
                eventsListData.setHasRead(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.HAS_READ)));
                eventsListData.setCreateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CREATE_TIME)));
                eventsListData.setUserId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_ID)));
                eventsListData.setUserName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_NAME)));
                eventsListData.setMemberCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.MEMBER_COUNT)));
                eventsListData.setEventTitle(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_TITLE)));
                eventsListData.setContent1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT1)));
                eventsListData.setContent2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT2)));
                eventsListData.setContent3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT3)));
                eventsListData.setInitTitle1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE1)));
                eventsListData.setInitTitle2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE2)));
                eventsListData.setInitTitle3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE3)));
                eventsListData.setUrl1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL1)));
                eventsListData.setUrl2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL2)));
                eventsListData.setUrl3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL3)));
                eventsListData.setReplyCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.REPLY_COUNT)));
                eventsListData.setReplyLastName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.REPLY_LAST_NAME)));
                eventsListData.setReplyLastContent(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.REPLY_LAST_CONTENT)));
                eventsListData.setThumbImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_THUMB_IMAGE)));
                eventsListData.setOriImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_ORI_IMAGE)));
                eventsListData.setUnReadCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.UNREAD_COUNT)));
                eventsListData.setAuthor(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.AUTHOR)));
                eventsListData.setEventAvatar(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_AVATAR)));
                eventsListData.setReceiver(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.RECEIVER)));
                eventsListData.setActivityTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ACTIVITY_TIME)));
                eventsListData.setActivitySite(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ACTIVITY_SITE)));
                eventsListData.setJoinCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.JOIN_COUNT)));
                eventsListData.setHasJoin(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.HAS_JOIN)));
                eventsListData.setVotes(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.VOTES)));
                eventsListData.setVoteMaxNumber(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.VOTE_NUMBER)));
                eventsListData.setEndTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.END_TIME)));
                eventsListData.setIsQrcode(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.IS_QRCODE)));
                eventsListData.setQrcode(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.QRCODE)));
                infos.add(eventsListData);
            }
            //db.setTransactionSuccessful();
        } catch (Exception e) {
            Log.e(TAG, " error  findEvents" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                //db.endTransaction();
                db.close();
            }
        }
        Log.i("EventList", "--所有size--" + infos.size());
        return infos;
    }

    /**
     * 按类型查找事件列表
     *
     * @return
     */
    public synchronized ArrayList<EventListData> findEventsByType(String type) {
        ArrayList<EventListData> infos = new ArrayList<EventListData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            if ("0".equals(type)) {
                cursor = db.query(CloudContactsDB.EventData.TABLE_NAME, null, null, null, null, null, CloudContactsDB.EventData.UPDATE_TIME + " desc");
            } else {
                cursor = db.query(CloudContactsDB.EventData.TABLE_NAME, null, "event_type = ? ", new String[]{type}, null, null, CloudContactsDB.EventData.UPDATE_TIME + " desc");
            }
            while (cursor.moveToNext()) {
                EventListData eventsListData = new EventListData();
                eventsListData.setEventId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_ID)));
                eventsListData.setUpdateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.UPDATE_TIME)));
                eventsListData.setEventType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_TYPE)));
                eventsListData.setEventName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_NAME)));
                eventsListData.setUserType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_TYPE)));
                eventsListData.setHasRead(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.HAS_READ)));
                eventsListData.setCreateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CREATE_TIME)));
                eventsListData.setUserId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_ID)));
                eventsListData.setUserName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_NAME)));
                eventsListData.setMemberCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.MEMBER_COUNT)));
                eventsListData.setEventTitle(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_TITLE)));
                eventsListData.setContent1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT1)));
                eventsListData.setContent2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT2)));
                eventsListData.setContent3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT3)));
                eventsListData.setInitTitle1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE1)));
                eventsListData.setInitTitle2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE2)));
                eventsListData.setInitTitle3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE3)));
                eventsListData.setUrl1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL1)));
                eventsListData.setUrl2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL2)));
                eventsListData.setUrl3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL3)));
                eventsListData.setReplyCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.REPLY_COUNT)));
                eventsListData.setReplyLastName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.REPLY_LAST_NAME)));
                eventsListData.setReplyLastContent(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.REPLY_LAST_CONTENT)));
                eventsListData.setUnReadCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.UNREAD_COUNT)));
                eventsListData.setThumbImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_THUMB_IMAGE)));
                eventsListData.setOriImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_ORI_IMAGE)));
                eventsListData.setAuthor(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.AUTHOR)));
                eventsListData.setEventAvatar(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_AVATAR)));
                eventsListData.setReceiver(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.RECEIVER)));
                eventsListData.setActivityTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ACTIVITY_TIME)));
                eventsListData.setActivitySite(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ACTIVITY_SITE)));
                eventsListData.setJoinCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.JOIN_COUNT)));
                eventsListData.setHasJoin(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.HAS_JOIN)));
                eventsListData.setVotes(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.VOTES)));
                eventsListData.setVoteMaxNumber(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.VOTE_NUMBER)));
                eventsListData.setEndTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.END_TIME)));
                eventsListData.setIsQrcode(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.IS_QRCODE)));
                eventsListData.setQrcode(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.QRCODE)));
                infos.add(eventsListData);
            }
        } catch (SQLException e) {
            Log.e(TAG, "error findEventsByType " + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        Log.i("EventList_query", "--type=" + type + "size--" + infos.size());
        return infos;
    }

    /**
     * 按EventId查找事件
     *
     * @return
     */
    public synchronized EventListData findOneEventById(String eventId) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        EventListData eventsListData = new EventListData();
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.EventData.TABLE_NAME, null, "event_id = ? ", new String[]{eventId}, null, null, null);
            while (cursor.moveToNext()) {
                eventsListData.setEventId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_ID)));
                eventsListData.setUpdateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.UPDATE_TIME)));
                eventsListData.setEventType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_TYPE)));
                eventsListData.setEventName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_NAME)));
                eventsListData.setUserType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_TYPE)));
                eventsListData.setHasRead(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.HAS_READ)));
                eventsListData.setCreateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CREATE_TIME)));
                eventsListData.setUserId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_ID)));
                eventsListData.setUserName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.USER_NAME)));
                eventsListData.setMemberCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.MEMBER_COUNT)));
                eventsListData.setEventTitle(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_TITLE)));
                eventsListData.setContent1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT1)));
                eventsListData.setContent2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT2)));
                eventsListData.setContent3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.CONTENT3)));
                eventsListData.setInitTitle1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE1)));
                eventsListData.setInitTitle2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE2)));
                eventsListData.setInitTitle3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.INIT_TITLE3)));
                eventsListData.setUrl1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL1)));
                eventsListData.setUrl2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL2)));
                eventsListData.setUrl3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.URL3)));
                eventsListData.setReplyCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.REPLY_COUNT)));
                eventsListData.setThumbImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_THUMB_IMAGE)));
                eventsListData.setOriImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_ORI_IMAGE)));
                eventsListData.setAuthor(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.AUTHOR)));
                eventsListData.setEventAvatar(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.EVENT_AVATAR)));
                eventsListData.setReceiver(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.RECEIVER)));
                eventsListData.setActivityTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ACTIVITY_TIME)));
                eventsListData.setActivitySite(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ACTIVITY_SITE)));
                eventsListData.setJoinCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.JOIN_COUNT)));
                eventsListData.setHasJoin(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.HAS_JOIN)));
                eventsListData.setVotes(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.VOTES)));
                eventsListData.setVoteMaxNumber(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.VOTE_NUMBER)));
            }
        } catch (SQLException e) {
            Log.e(TAG, "error findOneEventById" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return eventsListData;
    }

    /**
     * 按EventId查找,是否存在
     *
     * @return
     */
    public synchronized boolean isHaveEventById(String eventId) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.EventData.TABLE_NAME, null, "event_id = ? ", new String[]{eventId}, null, null, null);
            if (cursor.getCount() != 0) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            Log.e(TAG, "error isHaveEventById" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return false;
    }

    public synchronized long updateEventData(String eventId, EventListData eventListData) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.EventData.EVENT_ID, eventListData.getEventId());
            values.put(CloudContactsDB.EventData.UPDATE_TIME, eventListData.getUpdateTime());
            values.put(CloudContactsDB.EventData.EVENT_TYPE, eventListData.getEventType());
            values.put(CloudContactsDB.EventData.EVENT_NAME, eventListData.getEventName());
            values.put(CloudContactsDB.EventData.USER_TYPE, eventListData.getUserType());
            values.put(CloudContactsDB.EventData.HAS_READ, eventListData.getHasRead());
            values.put(CloudContactsDB.EventData.CREATE_TIME, eventListData.getCreateTime());
            values.put(CloudContactsDB.EventData.USER_ID, eventListData.getUserId());
            values.put(CloudContactsDB.EventData.USER_NAME, eventListData.getUserName());
            values.put(CloudContactsDB.EventData.MEMBER_COUNT, eventListData.getMemberCount());
            values.put(CloudContactsDB.EventData.EVENT_TITLE, eventListData.getEventTitle());
            values.put(CloudContactsDB.EventData.CONTENT1, eventListData.getContent1());
            values.put(CloudContactsDB.EventData.CONTENT2, eventListData.getContent2());
            values.put(CloudContactsDB.EventData.CONTENT3, eventListData.getContent3());
            values.put(CloudContactsDB.EventData.INIT_TITLE1, eventListData.getInitTitle1());
            values.put(CloudContactsDB.EventData.INIT_TITLE2, eventListData.getInitTitle2());
            values.put(CloudContactsDB.EventData.INIT_TITLE3, eventListData.getInitTitle3());
            values.put(CloudContactsDB.EventData.URL1, eventListData.getUrl1());
            values.put(CloudContactsDB.EventData.URL2, eventListData.getUrl2());
            values.put(CloudContactsDB.EventData.URL3, eventListData.getUrl3());
            values.put(CloudContactsDB.EventData.INIT_TITLE2, eventListData.getInitTitle2());
            values.put(CloudContactsDB.EventData.INIT_TITLE3, eventListData.getInitTitle3());
            values.put(CloudContactsDB.EventData.HAS_READ_COUNT, eventListData.getHasReadCount());
            values.put(CloudContactsDB.EventData.REPLY_COUNT, eventListData.getReplyCount());
            values.put(CloudContactsDB.EventData.REPLY_LAST_NAME, eventListData.getReplyLastName());
            values.put(CloudContactsDB.EventData.REPLY_LAST_CONTENT, eventListData.getReplyLastContent());
            values.put(CloudContactsDB.EventData.ATTACHMENT_THUMB_IMAGE, eventListData.getThumbImage());
            values.put(CloudContactsDB.EventData.ATTACHMENT_ORI_IMAGE, eventListData.getOriImage());
            values.put(CloudContactsDB.EventData.AUTHOR, eventListData.getAuthor());
            values.put(CloudContactsDB.EventData.EVENT_AVATAR, eventListData.getEventAvatar());
            values.put(CloudContactsDB.EventData.RECEIVER, eventListData.getReceiver());
            values.put(CloudContactsDB.EventData.ACTIVITY_TIME, eventListData.getActivityTime());
            values.put(CloudContactsDB.EventData.ACTIVITY_SITE, eventListData.getActivitySite());
            values.put(CloudContactsDB.EventData.JOIN_COUNT, eventListData.getJoinCount());
            values.put(CloudContactsDB.EventData.HAS_JOIN, eventListData.getHasJoin());
            values.put(CloudContactsDB.EventData.VOTES, eventListData.getVotes());
            flag = db.update(CloudContactsDB.EventData.TABLE_NAME, values, " event_id = ? ", new String[]{eventId});
        } catch (Exception e) {
            Log.e(TAG, "update_error" + e.toString());
        } finally {
            db.close();
        }
        Log.i(TAG, "--update_result--" + flag);
        return flag;
    }

    /**
     * 删除所有事件
     *
     * @return 非0表示成功，0表示失败
     */
    public synchronized boolean deleteAllEvents() {
        SQLiteDatabase db = helper.getWritableDatabase();
        int result = 0;
        try {
            result = db.delete(CloudContactsDB.EventData.TABLE_NAME, null, null);
        } catch (SQLException e) {
            Log.e(TAG, " error deleteAllEvents" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        return (result != 0);
    }

    /**
     * 删除一个事件
     *
     * @param eventId
     * @return 非0表示成功，0表示失败
     */
    public synchronized int deleteEventByEventId(String eventId) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int delete = 0;
        try {
            delete = db.delete(CloudContactsDB.EventData.TABLE_NAME, "event_id = ?", new String[]{eventId});
        } catch (SQLException e) {
            Log.e(TAG, "error deleteEventByEventId" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        //String sql = "delete + from _" + eventId + " where "+ event_id  + " = "+eventId ;
        // db.execSQL(sql);
        Log.i("delete", delete + "删除");
        return delete;
    }

    /**
     * 事件设置为非新事件
     *
     * @param eventId
     */
    public synchronized void updateEventNotNew(String eventId) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.EventData.HAS_READ, "1");
        String whereClause = "event_id = ? ";
        String[] whereArgs = {eventId};
        db.update(CloudContactsDB.EventData.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
        EventsFragment.isUpDate = true;
    }

    /**
     * 更新评论
     *
     * @param eventId
     */
    public synchronized void updateEventReply(EventReplyNewData eventReply, String eventId) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.EventData.REPLY_LAST_NAME, eventReply.getUserName());
        if (!StringUtil.isEmpty(eventReply.getContent())) {
            values.put(CloudContactsDB.EventData.REPLY_LAST_CONTENT, eventReply.getContent());
        } else {
            if (!StringUtil.isEmpty(eventReply.getAttachments())) {
                if (eventReply.getAttachments() != null) {
                    try {
                        org.json.JSONArray attachments = new org.json.JSONArray(eventReply.getAttachments());
                        JSONObject attachment = attachments.getJSONObject(0);
                        if ("1".equals(attachment.getString("type"))) {
                            values.put(CloudContactsDB.EventData.REPLY_LAST_CONTENT, "[图片]");
                        } else {
                            values.put(CloudContactsDB.EventData.REPLY_LAST_CONTENT, "[语音]");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        values.put(CloudContactsDB.EventData.UPDATE_TIME, eventReply.getUpdateTime());
        String whereClause = "event_id = ? ";
        String[] whereArgs = {eventId};
        db.update(CloudContactsDB.EventData.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
        EventsFragment.isUpDate = true;
    }


    //-===============event_model_table事件模板表=========================

    /**
     * 查找所有的事件模板
     *
     * @return
     */
    public synchronized List<EventListData> findAllEventsModel() {
        List<EventListData> infos = new ArrayList<EventListData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        //db.beginTransaction();
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.EventModelData.TABLE_NAME, null, null, null, null, null, null);
            while (cursor.moveToNext()) {
                EventListData eventsListData = new EventListData();
                eventsListData.setEventId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_ID)));
                eventsListData.setEventType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_TYPE)));
                eventsListData.setEventName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_TYPE_NAME)));
                eventsListData.setEventTitle(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_TITLE)));
                eventsListData.setReceiver(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_RECEIVER)));
                eventsListData.setUpdateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_TIME)));
                eventsListData.setContent1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.CONTENT1)));
                eventsListData.setContent2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.CONTENT2)));
                eventsListData.setContent3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.CONTENT3)));
                eventsListData.setInitTitle1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.INIT_TITLE1)));
                eventsListData.setInitTitle2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.INIT_TITLE2)));
                eventsListData.setInitTitle3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.INIT_TITLE3)));
                eventsListData.setThumbImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_THUMB_IMAGE)));
                eventsListData.setOriImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_ORI_IMAGE)));
                infos.add(eventsListData);
            }
            //db.setTransactionSuccessful();
        } catch (Exception e) {
            //e.printStackTrace();
            Log.e(TAG, "error findAllEventsByWhere " + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                //db.endTransaction();
                db.close();
            }
            Log.i("find_event", "查找到：  " + infos.size());
        }
        return infos;
    }


    /**
     * 根据 event_id 查找是否存在事件的模板
     *
     * @return
     */

    public synchronized EventListData findEventModeByEventId(String eventId) {
        EventListData eventsListData = new EventListData();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.EventModelData.TABLE_NAME, null, "event_id = ? ", new String[]{eventId}, null, null, null);
            while (cursor.moveToNext()) {
                eventsListData.setEventId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_ID)));
                eventsListData.setEventType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_TYPE)));
                eventsListData.setEventName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_TYPE_NAME)));
                eventsListData.setEventTitle(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_TITLE)));
                eventsListData.setReceiver(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_RECEIVER)));
                eventsListData.setUpdateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.EVENT_TIME)));
                eventsListData.setContent1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.CONTENT1)));
                eventsListData.setContent2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.CONTENT2)));
                eventsListData.setContent3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.CONTENT3)));
                eventsListData.setInitTitle1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.INIT_TITLE1)));
                eventsListData.setInitTitle2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.INIT_TITLE2)));
                eventsListData.setInitTitle3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventModelData.INIT_TITLE3)));
                eventsListData.setThumbImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_THUMB_IMAGE)));
                eventsListData.setOriImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_ORI_IMAGE)));
            }
        } catch (SQLException e) {
            Log.e(TAG, "error findEventModeByEventId" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return eventsListData;
    }

    /**
     * 删除一个模板
     *
     * @param eventId
     * @return 非0表示成功，0表示失败
     */
    public int deleteEventModelByEventId(String eventId) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.EventModelData.TABLE_NAME, "event_id = ?", new String[]{eventId});
        } catch (SQLException e) {
            Log.e(TAG, " error deleteEventModelByEventId " + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete_model", delete + "删除");
        return delete;
    }


    /**
     * 删除所有模板
     *
     * @return 非0表示成功，0表示失败
     */
    public int deleteEventModel() {
        SQLiteDatabase db = helper.getWritableDatabase();
        int delete = db.delete(CloudContactsDB.EventModelData.TABLE_NAME, null, null);
        db.close();
        Log.i("delete_model", delete + "删除");
        return delete;
    }


    /**
     * 添加一个事件模板
     *
     * @return -1表示失败，其它表示成功
     */
    public synchronized long insertEventModel(EventListData eventsListData) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        long flag = 0l;
        try {
            values.put(CloudContactsDB.EventModelData.EVENT_ID, eventsListData.getEventId());
            values.put(CloudContactsDB.EventModelData.EVENT_TYPE, eventsListData.getEventType());
            values.put(CloudContactsDB.EventModelData.EVENT_TYPE_NAME, eventsListData.getEventName());
            values.put(CloudContactsDB.EventModelData.EVENT_TITLE, eventsListData.getEventTitle());
            values.put(CloudContactsDB.EventModelData.EVENT_RECEIVER, eventsListData.getReceiver());
            values.put(CloudContactsDB.EventModelData.EVENT_TIME, eventsListData.getUpdateTime());
            values.put(CloudContactsDB.EventModelData.CONTENT1, eventsListData.getContent1());
            values.put(CloudContactsDB.EventModelData.CONTENT2, eventsListData.getContent2());
            values.put(CloudContactsDB.EventModelData.CONTENT3, eventsListData.getContent3());
            values.put(CloudContactsDB.EventModelData.INIT_TITLE1, eventsListData.getInitTitle1());
            values.put(CloudContactsDB.EventModelData.INIT_TITLE2, eventsListData.getInitTitle2());
            values.put(CloudContactsDB.EventModelData.INIT_TITLE3, eventsListData.getInitTitle3());
            values.put(CloudContactsDB.EventData.ATTACHMENT_THUMB_IMAGE, eventsListData.getThumbImage());
            values.put(CloudContactsDB.EventData.ATTACHMENT_ORI_IMAGE, eventsListData.getOriImage());
            values.put(CloudContactsDB.EventModelData.USER_ID, eventsListData.getUserId());
            flag = db.insert(CloudContactsDB.EventModelData.TABLE_NAME, null, values);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
        return flag;
    }

    /**
     * 更新一个事件模板
     *
     * @return -1表示失败，其它表示成功
     */
    public synchronized long updataEventModel(String eventId, EventListData eventsListData) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.EventModelData.EVENT_ID, eventsListData.getEventId());
        values.put(CloudContactsDB.EventModelData.EVENT_TYPE, eventsListData.getEventType());
        values.put(CloudContactsDB.EventModelData.EVENT_TYPE_NAME, eventsListData.getEventName());
        values.put(CloudContactsDB.EventModelData.EVENT_TITLE, eventsListData.getEventTitle());
        values.put(CloudContactsDB.EventModelData.EVENT_RECEIVER, eventsListData.getReceiver());
        values.put(CloudContactsDB.EventModelData.EVENT_TIME, eventsListData.getUpdateTime());
        values.put(CloudContactsDB.EventModelData.CONTENT1, eventsListData.getContent1());
        values.put(CloudContactsDB.EventModelData.CONTENT2, eventsListData.getContent2());
        values.put(CloudContactsDB.EventModelData.CONTENT3, eventsListData.getContent3());
        values.put(CloudContactsDB.EventModelData.INIT_TITLE1, eventsListData.getInitTitle1());
        values.put(CloudContactsDB.EventModelData.INIT_TITLE2, eventsListData.getInitTitle2());
        values.put(CloudContactsDB.EventModelData.INIT_TITLE3, eventsListData.getInitTitle3());
        values.put(CloudContactsDB.EventModelData.ATTACHMENT_THUMB_IMAGE, eventsListData.getThumbImage());
        values.put(CloudContactsDB.EventModelData.ATTACHMENT_ORI_IMAGE, eventsListData.getOriImage());
        int update = db.update(CloudContactsDB.EventModelData.TABLE_NAME, values, " event_id = ? ", new String[]{eventId});

        db.close();
        Log.i("updata_result", update + "");
        return update;
    }

    /**
     * 根据eventId 判断是否有这个模板
     *
     * @param eventId
     * @return
     */
    public synchronized boolean queryEventModelById(String eventId) {
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query(CloudContactsDB.EventModelData.TABLE_NAME, null, "event_id = ? ", new String[]{eventId}, null, null, null);
        if (cursor.getCount() != 0) {
            db.close();
            return true;
        } else {
            db.close();
            return false;
        }
    }

    /**
     * 按 eventType 查找事件模板
     *
     * @return
     */
    public synchronized boolean queryEventByType(String eventType) {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(CloudContactsDB.EventModelData.TABLE_NAME, null, "event_type_name = ? ", new String[]{eventType}, null, null, null);
        if (cursor.getCount() != 0) {
            db.close();
            return true;
        } else {
            db.close();
            return false;
        }
    }

    //=====================草稿箱=========================//

    /**
     * 添加一个事件草稿箱
     *
     * @return -1表示失败，其它表示成功
     */
    public synchronized long insertEventDrafts(EventListData eventsListData) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        long flag = 0l;
        try {
            values.put(CloudContactsDB.EventDraftsData.EVENT_ID, eventsListData.getEventId());
            values.put(CloudContactsDB.EventDraftsData.EVENT_TYPE, eventsListData.getEventType());
            values.put(CloudContactsDB.EventDraftsData.EVENT_TYPE_NAME, eventsListData.getEventName());
            values.put(CloudContactsDB.EventDraftsData.EVENT_TITLE, eventsListData.getEventTitle());
            values.put(CloudContactsDB.EventDraftsData.EVENT_RECEIVER, eventsListData.getReceiver());
            values.put(CloudContactsDB.EventDraftsData.EVENT_TIME, eventsListData.getUpdateTime());
            values.put(CloudContactsDB.EventDraftsData.CONTENT1, eventsListData.getContent1());
            values.put(CloudContactsDB.EventDraftsData.CONTENT2, eventsListData.getContent2());
            values.put(CloudContactsDB.EventDraftsData.CONTENT3, eventsListData.getContent3());
            values.put(CloudContactsDB.EventDraftsData.INIT_TITLE1, eventsListData.getInitTitle1());
            values.put(CloudContactsDB.EventDraftsData.INIT_TITLE2, eventsListData.getInitTitle2());
            values.put(CloudContactsDB.EventDraftsData.INIT_TITLE3, eventsListData.getInitTitle3());
            values.put(CloudContactsDB.EventDraftsData.ATTACHMENT_THUMB_IMAGE, eventsListData.getThumbImage());
            values.put(CloudContactsDB.EventDraftsData.ATTACHMENT_ORI_IMAGE, eventsListData.getOriImage());
            values.put(CloudContactsDB.EventDraftsData.USER_ID, eventsListData.getUserId());
            values.put(CloudContactsDB.EventDraftsData.AUTHOR, eventsListData.getAuthor());
            values.put(CloudContactsDB.EventDraftsData.ACTIVITY_SITE, eventsListData.getActivitySite());
            values.put(CloudContactsDB.EventDraftsData.ACTIVITY_TIME, eventsListData.getActivityTime());
            values.put(CloudContactsDB.EventDraftsData.END_TIME, eventsListData.getEndTime());
            values.put(CloudContactsDB.EventDraftsData.IS_QRCODE, eventsListData.getIsQrcode());
            flag = db.insert(CloudContactsDB.EventDraftsData.TABLE_NAME, null, values);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
        return flag;
    }

    /**
     * 删除一个草稿箱
     *
     * @param eventId
     * @return 非0表示成功，0表示失败
     */
    public int deleteEventDraftsByEventId(String eventId) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.EventDraftsData.TABLE_NAME, "event_id = ?", new String[]{eventId});
        } catch (SQLException e) {
            Log.e(TAG, " error deleteEventDraftsByEventId " + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete_drafts", delete + "删除");
        return delete;
    }

    /**
     * 更新一个事件草稿箱
     *
     * @return -1表示失败，其它表示成功
     */
    public synchronized long updataEventDrafts(String eventId, EventListData eventsListData) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.EventDraftsData.EVENT_ID, eventsListData.getEventId());
        values.put(CloudContactsDB.EventDraftsData.EVENT_TYPE, eventsListData.getEventType());
        values.put(CloudContactsDB.EventDraftsData.EVENT_TYPE_NAME, eventsListData.getEventName());
        values.put(CloudContactsDB.EventDraftsData.EVENT_TITLE, eventsListData.getEventTitle());
        values.put(CloudContactsDB.EventDraftsData.EVENT_RECEIVER, eventsListData.getReceiver());
        values.put(CloudContactsDB.EventDraftsData.EVENT_TIME, eventsListData.getUpdateTime());
        values.put(CloudContactsDB.EventDraftsData.CONTENT1, eventsListData.getContent1());
        values.put(CloudContactsDB.EventDraftsData.CONTENT2, eventsListData.getContent2());
        values.put(CloudContactsDB.EventDraftsData.CONTENT3, eventsListData.getContent3());
        values.put(CloudContactsDB.EventDraftsData.INIT_TITLE1, eventsListData.getInitTitle1());
        values.put(CloudContactsDB.EventDraftsData.INIT_TITLE2, eventsListData.getInitTitle2());
        values.put(CloudContactsDB.EventDraftsData.INIT_TITLE3, eventsListData.getInitTitle3());
        values.put(CloudContactsDB.EventDraftsData.ATTACHMENT_THUMB_IMAGE, eventsListData.getThumbImage());
        values.put(CloudContactsDB.EventDraftsData.ATTACHMENT_ORI_IMAGE, eventsListData.getOriImage());
        values.put(CloudContactsDB.EventDraftsData.AUTHOR, eventsListData.getAuthor());
        values.put(CloudContactsDB.EventDraftsData.ACTIVITY_SITE, eventsListData.getActivitySite());
        values.put(CloudContactsDB.EventDraftsData.ACTIVITY_TIME, eventsListData.getActivityTime());
        values.put(CloudContactsDB.EventDraftsData.END_TIME, eventsListData.getEndTime());
        values.put(CloudContactsDB.EventDraftsData.IS_QRCODE, eventsListData.getIsQrcode());
        int update = db.update(CloudContactsDB.EventDraftsData.TABLE_NAME, values, " event_id = ? ", new String[]{eventId});

        db.close();
        Log.i("updata_result", update + "");
        return update;
    }

    /**
     * 查找该用户所有的事件草稿箱
     * userId  用户UID
     *
     * @return
     */
    public synchronized List<EventListData> findAllEventsDrafts(String userId) {
        List<EventListData> infos = new ArrayList<EventListData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.EventDraftsData.TABLE_NAME, null, " user_id = ? ", new String[]{userId}, null, null, null);
            //Cursor cursor = db.query(CloudContactsDB.EventModelData.TABLE_NAME, null, " event_where = ? ", new String[]{where}, null, null, null);
            while (cursor.moveToNext()) {
                EventListData eventsListData = new EventListData();
                eventsListData.setEventId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.EVENT_ID)));
                eventsListData.setEventType(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.EVENT_TYPE)));
                eventsListData.setEventName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.EVENT_TYPE_NAME)));
                eventsListData.setEventTitle(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.EVENT_TITLE)));
                eventsListData.setReceiver(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.EVENT_RECEIVER)));
                eventsListData.setUpdateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.EVENT_TIME)));
                eventsListData.setContent1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.CONTENT1)));
                eventsListData.setContent2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.CONTENT2)));
                eventsListData.setContent3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.CONTENT3)));
                eventsListData.setInitTitle1(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.INIT_TITLE1)));
                eventsListData.setInitTitle2(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.INIT_TITLE2)));
                eventsListData.setInitTitle3(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.INIT_TITLE3)));
                eventsListData.setThumbImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_THUMB_IMAGE)));
                eventsListData.setOriImage(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventData.ATTACHMENT_ORI_IMAGE)));
                eventsListData.setUserId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.USER_ID)));
                eventsListData.setAuthor(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.AUTHOR)));
                eventsListData.setActivityTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.ACTIVITY_TIME)));
                eventsListData.setEndTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.END_TIME)));
                eventsListData.setIsQrcode(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.IS_QRCODE)));
                eventsListData.setActivitySite(cursor.getString(cursor.getColumnIndex(CloudContactsDB.EventDraftsData.ACTIVITY_SITE)));
                infos.add(eventsListData);
            }
        } catch (SQLException e) {
            Log.e(TAG, "error findAllEventsDrafts" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        Log.i("find_event", "查找到：  " + infos.size());
        return infos;
    }

    /**
     * 根据eventId 判断是否有这个草稿
     *
     * @param eventId
     * @return
     */
    public synchronized boolean queryEventDraftsById(String eventId) {
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query(CloudContactsDB.EventDraftsData.TABLE_NAME, null, "event_id = ? ", new String[]{eventId}, null, null, null);
        if (cursor.getCount() != 0) {
            db.close();
            return true;
        } else {
            db.close();
            return false;
        }
    }


    //======================================================//


    /**
     * 更新事件未读消息数
     *
     * @param eventID     事件id
     * @param unReadCount 未读消息数
     */
    public synchronized void updateEventUnReadCount(String eventID, String unReadCount) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.EventData.UNREAD_COUNT, unReadCount);
        String whereClause = "event_id = ? ";
        String[] whereArgs = {eventID};
        db.update(CloudContactsDB.EventData.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
    }

    /**
     * 更新事件活动参加情况
     *
     * @param eventID    事件id
     * @param applyCount 参加活动的人数
     */
    public synchronized void updateEventApplyCount(String eventID, String applyCount, String hasJoin) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.EventData.JOIN_COUNT, applyCount);
        values.put(CloudContactsDB.EventData.HAS_JOIN, hasJoin);
        String whereClause = "event_id = ? ";
        String[] whereArgs = {eventID};
        int update = db.update(CloudContactsDB.EventData.TABLE_NAME, values, whereClause, whereArgs);
        Log.d("activity", update + "");
        db.close();
        EventsFragment.isUpDate = true;
    }

    /**
     * 更新事件投票情况
     *
     * @param eventID 事件id
     * @param vote    投票选项
     */
    public synchronized void updateEventVote(String eventID, String vote) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.EventData.VOTES, vote);
        String whereClause = "event_id = ? ";
        String[] whereArgs = {eventID};
        int update = db.update(CloudContactsDB.EventData.TABLE_NAME, values, whereClause, whereArgs);
        Log.d("activity", update + "");
        db.close();
        EventsFragment.isUpDate = true;
    }

    //=====================contact_table======================//

    /**
     * 添加一个联系人信息
     *
     * @param phone
     * @param groupName
     * @param contactName
     * @param groupId
     * @return
     */
    public synchronized long insertContact(String uid, String phone, String job, String groupName, String contactName, String groupId) {
        SQLiteDatabase db = null;
        long flag = 0;
        try {
            db = helper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(CloudContactsDB.ContactData.MY_USER_ID, uid);
            values.put(CloudContactsDB.ContactData.CONTACT_NAME, contactName);
            values.put(CloudContactsDB.ContactData.CONTACT_PHONE, phone);
            values.put(CloudContactsDB.ContactData.CONTACT_JOB, job);
            values.put(CloudContactsDB.ContactData.CONTACT_GROUP, groupName);
            values.put(CloudContactsDB.ContactData.CONTACT_GROUP_ID, groupId);
            flag = db.insert(CloudContactsDB.ContactData.TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e("insertContact", " insertContact  失败");
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.d("Contact_msg", "--insert_result--" + flag + " " + contactName + phone + groupName + groupId);
        return flag;
    }

    /**
     * 查询一个联系人
     *
     * @param phone
     * @return
     */
    public synchronized List<String> findOneContact(String uid, String phone) {
        SQLiteDatabase db = null;
        List<String> contact = new ArrayList<String>();
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.ContactData.TABLE_NAME, null, "contact_phone = ? and my_user_id = ? ", new String[]{phone, uid}, null, null, null);
            if (cursor.moveToNext()) {
                contact.add(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ContactData.CONTACT_GROUP)));
                contact.add(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ContactData.CONTACT_NAME)));
                contact.add(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ContactData.CONTACT_JOB)));
            }
        } catch (Exception e) {
            Log.e("findOneContact", e.toString());
        } finally {
            if (db != null) {
                db.close();
            }
            if (cursor != null) {
                cursor.close();
            }
        }
        return contact;
    }

    /**
     * 查询是否已经存在
     *
     * @param uid
     * @param phone
     * @param groupId
     * @return
     */
    public synchronized List<String> findOneContact(String uid, String phone, String groupId) {
        SQLiteDatabase db = null;
        List<String> contact = new ArrayList<String>();
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.ContactData.TABLE_NAME, null, "contact_phone = ? and my_user_id = ? and contact_group_id = ? ", new String[]{phone, uid, groupId}, null, null, null);
            if (cursor.moveToNext()) {
                contact.add(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ContactData.CONTACT_GROUP)));
                contact.add(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ContactData.CONTACT_NAME)));
                contact.add(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ContactData.CONTACT_JOB)));
            }
        } catch (Exception e) {
            Log.e("findOneContact", e.toString());
        } finally {
            if (db != null) {
                db.close();
            }
            if (cursor != null) {
                cursor.close();
            }
        }
        return contact;
    }

    /**
     * 修改联系人
     *
     * @param uid
     * @param phone
     * @param groupName
     * @param contactName
     * @param groupId
     * @return
     */
    public synchronized boolean updateContact(String uid, String phone, String job, String groupName, String contactName, String groupId) {
        SQLiteDatabase db = null;
        int update = 0;
        try {
            db = helper.getWritableDatabase();
            ContentValues values = new ContentValues();
            //values.put(CloudContactsDB.ContactData.MY_USER_ID, uid);
            //values.put(CloudContactsDB.ContactData.CONTACT_PHONE, phone);
            //values.put(CloudContactsDB.ContactData.CONTACT_GROUP_ID, groupId);
            values.put(CloudContactsDB.ContactData.CONTACT_GROUP, groupName);
            values.put(CloudContactsDB.ContactData.CONTACT_NAME, contactName);
            values.put(CloudContactsDB.ContactData.CONTACT_JOB, job);
            update = db.update(CloudContactsDB.ContactData.TABLE_NAME, values, " contact_phone = ? and my_user_id = ? and contact_group_id = ? ", new String[]{phone, uid, groupId});

        } catch (Exception e) {
            Log.e("updateContact", e.toString());
        } finally {
            if (db != null) {
                db.close();
            }
        }
        return (update != 0);
    }

    public synchronized boolean deleteContactByGroupId(String uid, String groupId) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.ContactData.TABLE_NAME, " my_user_id = ? and contact_group_id = ? ", new String[]{uid, groupId});
        } catch (Exception e) {
            Log.e("deleteContactByGroupId", e.toString());
        } finally {
            if (db != null) {
                db.close();
            }
        }
        return (delete != 0);

    }

    public synchronized boolean deleteContactByPhone(String uid, String phone) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.ContactData.TABLE_NAME, " my_user_id = ? and contact_phone = ? ", new String[]{uid, phone});

        } catch (Exception e) {
            Log.e("deleteContactByPhone", e.toString());
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.d("deleteContactByPhone", delete + "删除我的contact");
        return (delete != 0);

    }


    //=====================user_table======================//

    /**
     * 根据UserId查询用户信息
     *
     * @return
     */
    public synchronized ChatContact findUserMsgById(String userId) {
        SQLiteDatabase db = helper.getReadableDatabase();
        ChatContact chatContact = new ChatContact();
        Cursor cursor = db.query(CloudContactsDB.UserData.TABLE_NAME, null, "user_id = ? ", new String[]{userId}, null, null, null);
        while (cursor.moveToNext()) {
            chatContact.userId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.UserData.USER_ID));
            chatContact.userName = cursor.getString(cursor.getColumnIndex(CloudContactsDB.UserData.USER_NAME));
            chatContact.userImg = cursor.getString(cursor.getColumnIndex(CloudContactsDB.UserData.USER_IMG));
        }
        cursor.close();
        db.close();
        Log.i("User", "--findUserMsg--userId=" + userId + "cloudContact--" + chatContact.userName);
        return chatContact;
    }


    /**
     * 添加一个用户信息
     *
     * @return -1表示失败，其它表示成功
     */
    public synchronized long insertUserMsg(ChatContact chatContact) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.UserData.USER_ID, chatContact.userId);
        values.put(CloudContactsDB.UserData.USER_NAME, chatContact.userName);
        values.put(CloudContactsDB.UserData.USER_IMG, chatContact.userImg);
        long flag = db.insert(CloudContactsDB.UserData.TABLE_NAME, null, values);
        db.close();
        Log.i("User", "--insert_result--" + flag);
        return flag;
    }

    /**
     * 更新一个用户的信息
     *
     * @return -1表示失败，其它表示成功
     */
    public synchronized long updataUserMsg(String userId, ChatContact chatContact) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.UserData.USER_NAME, chatContact.userName);
        values.put(CloudContactsDB.UserData.USER_IMG, chatContact.userImg);
        int update = db.update(CloudContactsDB.UserData.TABLE_NAME, values, " user_id = ? ", new String[]{userId});
        db.close();
        Log.i("User", "--updata_result--" + update + "");
        return update;
    }


    //=====================talk_my_msg_table======================//

    public ArrayList<MyTalkMessageData> getTalkMyMsgData() {
        ArrayList<MyTalkMessageData> localData = new ArrayList<MyTalkMessageData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.TalkMyMsgData.TABLE_NAME, null, null, null, null, null, CloudContactsDB.TalkMyMsgData.MSG_CTIME + " desc");
            while (cursor.moveToNext()) {
                MyTalkMessageData talkMessageData = new MyTalkMessageData();
                talkMessageData._id = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData._ID));
                talkMessageData.talkId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.TALK_ID));
                talkMessageData.msgType = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.MSG_TYPE));
                talkMessageData.msgUId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.MSG_UID));
                talkMessageData.msgUName = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.MSG_UNAME));
                talkMessageData.msgUImg = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.MSG_UIMG));
                talkMessageData.msgRecvUId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.MSG_RECV_UID));
                talkMessageData.msgRecvUName = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.MSG_RECV_UNAME));
                talkMessageData.msgComment = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.MSG_COMMENT));
                talkMessageData.msgCTime = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.MSG_CTIME));
                talkMessageData.talkThumb = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.TALK_THUMB));
                talkMessageData.talkUId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.TALK_UID));
                talkMessageData.talkUName = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.TALK_UNAME));
                talkMessageData.talkUImage = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.TALK_UIMG));
                talkMessageData.text = cursor.getString(cursor.getColumnIndex(CloudContactsDB.TalkMyMsgData.TALK_TEXT));
                localData.add(talkMessageData);
            }
        } catch (Exception e) {
            Log.e(TAG, "findTalk  error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return localData;
    }


    public synchronized long insertTalkMyMsgData(MyTalkMessageData talkMessageData) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.TalkMyMsgData.TALK_ID, talkMessageData.talkId);
            values.put(CloudContactsDB.TalkMyMsgData.MSG_TYPE, talkMessageData.msgType);
            values.put(CloudContactsDB.TalkMyMsgData.MSG_UID, talkMessageData.msgUId);
            values.put(CloudContactsDB.TalkMyMsgData.MSG_UNAME, talkMessageData.msgUName);
            values.put(CloudContactsDB.TalkMyMsgData.MSG_UIMG, talkMessageData.msgUImg);
            values.put(CloudContactsDB.TalkMyMsgData.MSG_RECV_UID, talkMessageData.msgRecvUId);
            values.put(CloudContactsDB.TalkMyMsgData.MSG_RECV_UNAME, talkMessageData.msgRecvUName);
            values.put(CloudContactsDB.TalkMyMsgData.MSG_COMMENT, talkMessageData.msgComment);
            values.put(CloudContactsDB.TalkMyMsgData.MSG_CTIME, talkMessageData.msgCTime);
            values.put(CloudContactsDB.TalkMyMsgData.TALK_THUMB, talkMessageData.talkThumb);
            values.put(CloudContactsDB.TalkMyMsgData.TALK_UID, talkMessageData.talkUId);
            values.put(CloudContactsDB.TalkMyMsgData.TALK_UNAME, talkMessageData.talkUName);
            values.put(CloudContactsDB.TalkMyMsgData.TALK_UIMG, talkMessageData.talkUImage);
            values.put(CloudContactsDB.TalkMyMsgData.TALK_TEXT, talkMessageData.text);
            flag = db.insert(CloudContactsDB.TalkMyMsgData.TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e(TAG, "insert_error" + e.toString());
        } finally {
            db.close();
        }
        Log.i("Talk", "--insert_result--" + flag);
        return flag;
    }

    /**
     * 删除所有我的消息
     *
     * @return 非0表示成功，0表示失败
     */
    public int deleteAllTalkMyMsg() {
        SQLiteDatabase db = helper.getWritableDatabase();
        int delete = db.delete(CloudContactsDB.TalkMyMsgData.TABLE_NAME, null, null);
        db.close();
        Log.i("delete", delete + "删除我的消息");
        return delete;
    }


    /**
     * 删除一个我的消息
     *
     * @return 非0表示成功，0表示失败
     */
    public int deleteOneTalkMyMsg(String id) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.TalkMyMsgData.TABLE_NAME, "_id = ?", new String[]{id});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除我的消息" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete_model", delete + "删除");
        return delete;
    }

    //=====================alarm_warn_table======================//

    public synchronized long insertAlarmWarnData(ScheduleData scheduleData) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.AlarmWarnData.EVENT_ID, scheduleData.getId());
            values.put(CloudContactsDB.AlarmWarnData.EVENT_TITLE, scheduleData.getTitle());
            values.put(CloudContactsDB.AlarmWarnData.ADDRESS, scheduleData.getAddress());
            values.put(CloudContactsDB.AlarmWarnData.REMARK, scheduleData.getRemark());
            values.put(CloudContactsDB.AlarmWarnData.START_DATETIME, scheduleData.getStartTime());
            values.put(CloudContactsDB.AlarmWarnData.END_DATETIME, scheduleData.getEndTime());
            values.put(CloudContactsDB.AlarmWarnData.ALERT_DATETIME, scheduleData.getAlertTime());
            values.put(CloudContactsDB.AlarmWarnData.CREATE_TIME, scheduleData.getCreateTime());
            values.put(CloudContactsDB.AlarmWarnData.UPDATE_TIME, scheduleData.getUpdateTime());
            values.put(CloudContactsDB.AlarmWarnData.BOSS_UID, scheduleData.getBossUid());
            values.put(CloudContactsDB.AlarmWarnData.DELEGATE_USER, scheduleData.getPublishedUId());
            values.put(CloudContactsDB.AlarmWarnData.DELEGATE_USERNAME, scheduleData.getPublishedUName());
            values.put(CloudContactsDB.AlarmWarnData.NOTICE, scheduleData.getNotice());
            flag = db.insert(CloudContactsDB.AlarmWarnData.TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e(TAG, "insert_error" + e.toString());
        } finally {
            db.close();
        }
        Log.i("Talk", "--insert_result--" + flag);
        return flag;
    }


    public ArrayList<ScheduleData> getAlarmWarnData() {
        ArrayList<ScheduleData> localData = new ArrayList<ScheduleData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.AlarmWarnData.TABLE_NAME, null, null, null, null, null, null);
            while (cursor.moveToNext()) {
                ScheduleData scheduleData = new ScheduleData();
                scheduleData.setId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.EVENT_ID)));
                scheduleData.setTitle(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.EVENT_TITLE)));
                scheduleData.setAddress(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.ADDRESS)));
                scheduleData.setRemark(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.REMARK)));
                scheduleData.setStartTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.START_DATETIME)));
                scheduleData.setEndTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.END_DATETIME)));
                scheduleData.setAlertTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.ALERT_DATETIME)));
                scheduleData.setCreateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.CREATE_TIME)));
                scheduleData.setUpdateTime(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.UPDATE_TIME)));
                scheduleData.setBossUid(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.BOSS_UID)));
                scheduleData.setPublishedUId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.DELEGATE_USER)));
                scheduleData.setPublishedUName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.DELEGATE_USERNAME)));
                scheduleData.setNotice(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AlarmWarnData.NOTICE)));
                localData.add(scheduleData);
            }
        } catch (Exception e) {
            Log.e(TAG, "findTalk  error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return localData;
    }


    /**
     * 更新一个事件草稿箱
     *
     * @return -1表示失败，其它表示成功
     */
    public synchronized long updataAlarmWarn(String eventId, ScheduleData scheduleData) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CloudContactsDB.AlarmWarnData.EVENT_ID, scheduleData.getId());
        values.put(CloudContactsDB.AlarmWarnData.EVENT_TITLE, scheduleData.getTitle());
        values.put(CloudContactsDB.AlarmWarnData.ADDRESS, scheduleData.getAddress());
        values.put(CloudContactsDB.AlarmWarnData.REMARK, scheduleData.getRemark());
        values.put(CloudContactsDB.AlarmWarnData.START_DATETIME, scheduleData.getStartTime());
        values.put(CloudContactsDB.AlarmWarnData.END_DATETIME, scheduleData.getEndTime());
        values.put(CloudContactsDB.AlarmWarnData.ALERT_DATETIME, scheduleData.getAlertTime());
        values.put(CloudContactsDB.AlarmWarnData.CREATE_TIME, scheduleData.getCreateTime());
        values.put(CloudContactsDB.AlarmWarnData.UPDATE_TIME, scheduleData.getUpdateTime());
        values.put(CloudContactsDB.AlarmWarnData.BOSS_UID, scheduleData.getBossUid());
        values.put(CloudContactsDB.AlarmWarnData.DELEGATE_USER, scheduleData.getPublishedUId());
        values.put(CloudContactsDB.AlarmWarnData.DELEGATE_USERNAME, scheduleData.getPublishedUName());
        values.put(CloudContactsDB.AlarmWarnData.NOTICE, scheduleData.getNotice());
        int update = db.update(CloudContactsDB.AlarmWarnData.TABLE_NAME, values, " event_id = ? ", new String[]{eventId});

        db.close();
        Log.i("updata_result", update + "");
        return update;
    }

    /**
     * 删除一个我的消息
     *
     * @return 非0表示成功，0表示失败
     */
    public int deleteOneAlarmWarn(String eventId) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.AlarmWarnData.TABLE_NAME, "event_id = ?", new String[]{eventId});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }


    //=====================calendar_table======================//

    public synchronized long insertCalendarListData(ScheduleListData scheduleListData) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.CalendarData.USER_ID, scheduleListData.getUserId());
            values.put(CloudContactsDB.CalendarData.YEAR, scheduleListData.getYear());
            values.put(CloudContactsDB.CalendarData.MONTH, scheduleListData.getMonth());
            values.put(CloudContactsDB.CalendarData.DAY, scheduleListData.getDay());
            values.put(CloudContactsDB.CalendarData.STATUS, scheduleListData.getStatus());
            values.put(CloudContactsDB.CalendarData.STATE, scheduleListData.getState());
            flag = db.insert(CloudContactsDB.CalendarData.TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e(TAG, "insert_error" + e.toString());
        } finally {
            db.close();
        }
        Log.i("Calendar", "--insert_result--" + flag);
        return flag;
    }

    public int deleteCalendarByDate(String uid, String year, String month) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.CalendarData.TABLE_NAME, " user_id = ? and year = ? and month = ? ", new String[]{uid, year, month});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }

    public ArrayList<ScheduleListData> findCalendarData(String uid, String year, String month) {
        ArrayList<ScheduleListData> localData = new ArrayList<ScheduleListData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();

            cursor = db.query(CloudContactsDB.CalendarData.TABLE_NAME, null, " user_id = ? and year = ? and month = ? ", new String[]{uid, year, month}, null, null, null);
            while (cursor.moveToNext()) {
                ScheduleListData scheduleListData = new ScheduleListData();
                scheduleListData.setYear(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CalendarData.YEAR)));
                scheduleListData.setMonth(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CalendarData.MONTH)));
                scheduleListData.setDay(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CalendarData.DAY)));
                scheduleListData.setStatus(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CalendarData.STATUS)));
                scheduleListData.setState(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CalendarData.STATE)));
                localData.add(scheduleListData);
            }
        } catch (Exception e) {
            Log.e(TAG, "findTalk  error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return localData;
    }


//-----------------------attendance_calendar_table---------------------

    public synchronized long insertAttendanceCalendarListData(AttendanceCalendarData attendanceCalendarData) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.AttendanceUserCalendarData.USER_ID, attendanceCalendarData.getUserId());
            values.put(CloudContactsDB.AttendanceUserCalendarData.YEAR, attendanceCalendarData.getYear());
            values.put(CloudContactsDB.AttendanceUserCalendarData.MONTH, attendanceCalendarData.getMonth());
            values.put(CloudContactsDB.AttendanceUserCalendarData.DAY, attendanceCalendarData.getDay());
            values.put(CloudContactsDB.AttendanceUserCalendarData.STATUS, attendanceCalendarData.getStatus());
            values.put(CloudContactsDB.AttendanceUserCalendarData.CHEAT, attendanceCalendarData.getCheat());
            values.put(CloudContactsDB.AttendanceUserCalendarData.GROUP_ID, attendanceCalendarData.getGroupId());
            values.put(CloudContactsDB.AttendanceUserCalendarData.IS_LEGWORK, attendanceCalendarData.getIsLegwork());
            flag = db.insert(CloudContactsDB.AttendanceUserCalendarData.TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e(TAG, "insert_error" + e.toString());
        } finally {
            db.close();
        }
        Log.i(TAG, "--insertAttendanceCalendarListData--" + flag);
        return flag;
    }

    public int deleteAttendanceCalendarByDate(String uid, String year, String month, String groupId) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.AttendanceUserCalendarData.TABLE_NAME, " user_id = ? and year = ? and month = ? and group_id = ? ", new String[]{uid, year, month, groupId});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }

    public ArrayList<AttendanceCalendarData> findAttendanceCalendarData(String uid, String year, String month, String groupId) {
        ArrayList<AttendanceCalendarData> localData = new ArrayList<AttendanceCalendarData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();

            cursor = db.query(CloudContactsDB.AttendanceUserCalendarData.TABLE_NAME, null, " user_id = ? and year = ? and month = ? and group_id = ? ", new String[]{uid, year, month, groupId}, null, null, null);
            while (cursor.moveToNext()) {
                AttendanceCalendarData attendanceCalendarData = new AttendanceCalendarData();
                attendanceCalendarData.setYear(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AttendanceUserCalendarData.YEAR)));
                attendanceCalendarData.setMonth(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AttendanceUserCalendarData.MONTH)));
                attendanceCalendarData.setDay(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AttendanceUserCalendarData.DAY)));
                attendanceCalendarData.setStatus(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AttendanceUserCalendarData.STATUS)));
                attendanceCalendarData.setCheat(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AttendanceUserCalendarData.CHEAT)));
                attendanceCalendarData.setGroupId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AttendanceUserCalendarData.GROUP_ID)));
                attendanceCalendarData.setIsLegwork(cursor.getString(cursor.getColumnIndex(CloudContactsDB.AttendanceUserCalendarData.IS_LEGWORK)));
                localData.add(attendanceCalendarData);
            }
        } catch (Exception e) {
            Log.e(TAG, "findAttendanceCalendarData  error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return localData;
    }

    //-----------------------approval_table---------------------


    public synchronized long insertApprovalListData(ApprovalData approvalData, int listType, String uid) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.ApprovalData.MY_USER_ID, uid);
            values.put(CloudContactsDB.ApprovalData.USER_ID, approvalData.getUser_id());
            values.put(CloudContactsDB.ApprovalData.USER_NAME, approvalData.getUser_name());
            values.put(CloudContactsDB.ApprovalData.USER_IMAGE, approvalData.getUser_image());

            values.put(CloudContactsDB.ApprovalData.APPROVAL_LIST_TYPE, listType);
            values.put(CloudContactsDB.ApprovalData.APPROVAL_ID, approvalData.getApproval_id());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_GROUP_ID, approvalData.getApproval_group_id());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_TYPE, approvalData.getApproval_type());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_LEAVE_TYPE, approvalData.getApproval_leave_type());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_ADDRESS, approvalData.getApproval_address());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_START_TIME, approvalData.getApproval_start_time());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_END_TIME, approvalData.getApproval_end_time());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_START_PART, approvalData.getApproval_start_part());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_END_PART, approvalData.getApproval_end_part());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_DAY, approvalData.getApproval_day());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_REASON, approvalData.getApproval_reason());

            values.put(CloudContactsDB.ApprovalData.APPROVE_ADMIN, approvalData.getApprove_admin());
            values.put(CloudContactsDB.ApprovalData.APPROVE_ADMIN_NAME, approvalData.getApprove_admin_name());
            values.put(CloudContactsDB.ApprovalData.APPROVE_ADMIN_IMAGE, approvalData.getApprove_admin_image());

            values.put(CloudContactsDB.ApprovalData.APPROVAL_DONE_TIME, approvalData.getApproval_done_time());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_TIME, approvalData.getApproval_time());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_MONTH, approvalData.getMonth());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_YEAR, approvalData.getYear());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_STATUS, approvalData.getApproval_status());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_REFUSE_REASON, approvalData.getApproval_refuse_reason());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_CANCEL, approvalData.getApproval_cancel());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_CANCEL_REASON, approvalData.getApproval_cancel_reason());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_CANCEL_TIME, approvalData.getApproval_cancel_time());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_CANCEL_DONE_TIME, approvalData.getApproval_cancel_done_time());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_DATES,approvalData.getApproval_dates());

            flag = db.insert(CloudContactsDB.ApprovalData.TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e(TAG, "insert_error" + e.toString());
        } finally {
            db.close();
        }
        Log.i(TAG, "--insertApprovalDataListData--" + flag);
        return flag;
    }

    public ArrayList<ApprovalData> findApprovalData(String uid, int listType, String groupId, String screen, ArrayList<String> args) {
        ArrayList<ApprovalData> localData = new ArrayList<ApprovalData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        String search = " my_user_id = ? and list_type = ? and approval_group_id = ? ";

        try {
            db = helper.getReadableDatabase();
            if (StringUtil.isEmpty(screen) || args.size() == 0) {
                cursor = db.query(CloudContactsDB.ApprovalData.TABLE_NAME, null, search, new String[]{uid, listType + "", groupId}, null, null, null);
            } else {
                String[] selectionArgs = new String[args.size() + 3];
                selectionArgs[0] = uid;
                selectionArgs[1] = listType + "";
                selectionArgs[2] = groupId;
                for (int i = 0; i < args.size(); i++) {
                    selectionArgs[3 + i] = args.get(i);
                }
                cursor = db.query(CloudContactsDB.ApprovalData.TABLE_NAME, null, search + screen, selectionArgs, null, null, null);
            }
            while (cursor.moveToNext()) {
                ApprovalData approvalData = new ApprovalData();
                approvalData.setUser_id(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.USER_ID)));
                approvalData.setUser_image(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.USER_IMAGE)));
                approvalData.setUser_name(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.USER_NAME)));

                approvalData.setApproval_id(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_ID)));
                approvalData.setApproval_group_id(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_GROUP_ID)));
                approvalData.setApproval_type(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_TYPE)));
                approvalData.setApproval_leave_type(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_LEAVE_TYPE)));
                approvalData.setApproval_address(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_ADDRESS)));
                approvalData.setApproval_start_time(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_START_TIME)));
                approvalData.setApproval_end_time(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_END_TIME)));
                approvalData.setApproval_start_part(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_START_PART)));
                approvalData.setApproval_end_part(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_END_PART)));
                approvalData.setApproval_day(cursor.getFloat(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_DAY)));
                approvalData.setApproval_reason(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_REASON)));

                approvalData.setApprove_admin(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVE_ADMIN)));
                approvalData.setApprove_admin_name(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVE_ADMIN_NAME)));
                approvalData.setApprove_admin_image(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVE_ADMIN_IMAGE)));

                approvalData.setApproval_done_time(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_DONE_TIME)));
                approvalData.setApproval_time(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_TIME)));
                approvalData.setApproval_status(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_STATUS)));
                approvalData.setMonth(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_MONTH)));
                approvalData.setYear(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_YEAR)));
                approvalData.setApproval_refuse_reason(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_REFUSE_REASON)));
                approvalData.setApproval_cancel(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_CANCEL)));
                approvalData.setApproval_cancel_reason(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_CANCEL_REASON)));
                approvalData.setApproval_cancel_time(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_CANCEL_TIME)));
                approvalData.setApproval_cancel_done_time(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_CANCEL_DONE_TIME)));
                approvalData.setApproval_dates(cursor.getString(cursor.getColumnIndex(CloudContactsDB.ApprovalData.APPROVAL_DATES)));
                localData.add(approvalData);
            }
            Log.d("approval_fragment", "   findApprovalData  " + localData.size());
        } catch (Exception e) {
            Log.e(TAG, "findAttendanceCalendarData  error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return localData;
    }

    public int upDateOneApproval(String uid, int listType, ApprovalData approvalData) {
        SQLiteDatabase db = null;
        int update = 0;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.ApprovalData.APPROVAL_DONE_TIME, approvalData.getApproval_done_time());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_STATUS, approvalData.getApproval_status());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_REFUSE_REASON, approvalData.getApproval_refuse_reason());
            update = db.update(CloudContactsDB.ApprovalData.TABLE_NAME, values, " my_user_id = ? and list_type = ? and approval_group_id = ? and approval_id = ? ", new String[]{uid, listType + "", approvalData.getApproval_group_id(), approvalData.getApproval_id()});
        } catch (SQLException e) {
            Log.i("upDateOneApproval", update + "更新" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("upDateOneApproval", update + "更新");
        return update;
    }

    public int upDateCancelApproval(String uid, int listType, ApprovalData approvalData) {
        SQLiteDatabase db = null;
        int update = 0;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.ApprovalData.APPROVAL_CANCEL_DONE_TIME, approvalData.getApproval_cancel_done_time());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_STATUS, approvalData.getApproval_status());
            values.put(CloudContactsDB.ApprovalData.APPROVAL_REFUSE_REASON, approvalData.getApproval_refuse_reason());
            update = db.update(CloudContactsDB.ApprovalData.TABLE_NAME, values, " my_user_id = ? and list_type = ? and approval_group_id = ? and approval_id = ? ", new String[]{uid, listType + "", approvalData.getApproval_group_id(), approvalData.getApproval_id()});
        } catch (SQLException e) {
            Log.i("upDateOneApproval", update + "更新" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("upDateOneApproval", update + "更新");
        return update;
    }

    public int deleteOneApproval(String uid, int listType, String groupId, String approvalId) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.ApprovalData.TABLE_NAME, " my_user_id = ? and list_type = ? and approval_group_id = ? and approval_id = ? ", new String[]{uid, listType + "", groupId, approvalId});
        } catch (SQLException e) {
            Log.i("deleteOneApproval", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("deleteOneApproval", delete + "删除");
        return delete;
    }

    public int deleteAllApproval(String uid, int listType, String groupId) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.ApprovalData.TABLE_NAME, " my_user_id = ? and list_type = ? and approval_group_id = ? ", new String[]{uid, listType + "", groupId});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }

    //-----------------------system_table---------------------

    public synchronized long insertSystemListData(SecretaryBean secretaryBean, String uid) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.SystemListData.USER_ID, uid);
            values.put(CloudContactsDB.SystemListData.ID, secretaryBean.getPush_id());
            values.put(CloudContactsDB.SystemListData.PUSH_TITLE, secretaryBean.getPush_title());
            values.put(CloudContactsDB.SystemListData.PUSH_TYPE, secretaryBean.getPush_type());
            values.put(CloudContactsDB.SystemListData.CREATE_TIME, secretaryBean.getCreate_time());
            values.put(CloudContactsDB.SystemListData.EXTRA, secretaryBean.getExtra());
            flag = db.insert(CloudContactsDB.SystemListData.TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e(TAG, "insert_error" + e.toString());
        } finally {
            db.close();
        }
        Log.i(TAG, "--insertSystemListData--" + flag);
        return flag;
    }

    public ArrayList<SecretaryBean> findSystemListData(String uid) {
        ArrayList<SecretaryBean> localData = new ArrayList<SecretaryBean>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.SystemListData.TABLE_NAME, null, " user_id = ? ", new String[]{uid}, null, null, null);
            while (cursor.moveToNext()) {
                SecretaryBean secretaryBean = new SecretaryBean();
                secretaryBean.setPush_id(cursor.getString(cursor.getColumnIndex(CloudContactsDB.SystemListData.ID)));
                secretaryBean.setPush_type(cursor.getString(cursor.getColumnIndex(CloudContactsDB.SystemListData.PUSH_TYPE)));
                secretaryBean.setPush_title(cursor.getString(cursor.getColumnIndex(CloudContactsDB.SystemListData.PUSH_TITLE)));
                secretaryBean.setCreate_time(cursor.getString(cursor.getColumnIndex(CloudContactsDB.SystemListData.CREATE_TIME)));
                secretaryBean.setExtra(cursor.getString(cursor.getColumnIndex(CloudContactsDB.SystemListData.EXTRA)));
                localData.add(secretaryBean);
            }
            Log.d(TAG, "   findSystemListData  " + localData.size());
        } catch (Exception e) {
            Log.e(TAG, "findSystemListData  error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return localData;
    }

    public int deleteAllSystemData(String uid) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.SystemListData.TABLE_NAME, " user_id = ? ", new String[]{uid});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }


    //-------------------------weekly_list_table---------------------------//
    public synchronized long insertWeeklyListData(WeeklyDataBean weeklyDataBean) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.WeeklyListData.WEEKLY_IDS, weeklyDataBean.getWeeklyIds());
            values.put(CloudContactsDB.WeeklyListData.WEEK_COUNT, weeklyDataBean.getWeekCount());
            values.put(CloudContactsDB.WeeklyListData.YEAR, weeklyDataBean.getWeeklyYear());
            values.put(CloudContactsDB.WeeklyListData.MONTH, weeklyDataBean.getWeeklyMonth());
            values.put(CloudContactsDB.WeeklyListData.GROUP_ID, weeklyDataBean.getGroupId());
            flag = db.insert(CloudContactsDB.WeeklyListData.TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e(TAG, "insert_error" + e.toString());
        } finally {
            db.close();
        }
        Log.i(TAG, "--weekly_list_table  insert--" + flag);
        return flag;
    }

    /**
     * @param type    0: 不限定年月条件   1：限定年份，不限定月份  2：限定月份，不限定年份  3：限定年份和月份
     * @param year
     * @param month
     * @param groupId
     * @return
     */
    public ArrayList<WeeklyDataBean> findWeeklyListData(int type, String year, String month, String groupId) {
        ArrayList<WeeklyDataBean> localData = new ArrayList<WeeklyDataBean>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            if (type == 0) {
                cursor = db.query(CloudContactsDB.WeeklyListData.TABLE_NAME, null, " group_id = ? ", new String[]{groupId}, null, null, null);
            } else if (type == 1) {
                cursor = db.query(CloudContactsDB.WeeklyListData.TABLE_NAME, null, " year = ? and group_id = ? ", new String[]{year, groupId}, null, null, null);

            } else if (type == 2) {
                cursor = db.query(CloudContactsDB.WeeklyListData.TABLE_NAME, null, " month = ? and group_id = ? ", new String[]{month, groupId}, null, null, null);

            } else if (type == 3) {
                cursor = db.query(CloudContactsDB.WeeklyListData.TABLE_NAME, null, " year = ? and month = ? and group_id = ? ", new String[]{year, month, groupId}, null, null, null);
            }
            while (cursor.moveToNext()) {
                WeeklyDataBean weeklyDataBean = new WeeklyDataBean();
                weeklyDataBean.setWeeklyYear(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyListData.YEAR)));
                weeklyDataBean.setWeeklyMonth(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyListData.MONTH)));
                weeklyDataBean.setWeekCount(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyListData.WEEK_COUNT)));
                weeklyDataBean.setWeeklyIds(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyListData.WEEKLY_IDS)));
                weeklyDataBean.setGroupId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyListData.GROUP_ID)));
                localData.add(weeklyDataBean);
            }
        } catch (Exception e) {
            Log.e(TAG, "findWeeklyListData  error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return localData;
    }


    public int deleteAllWeeklyList(String groupId) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.WeeklyListData.TABLE_NAME, " group_id = ? ", new String[]{groupId});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }

    public int deleteAllWeeklyList() {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.WeeklyListData.TABLE_NAME, null, null);
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }


    //-------------------------weekly_detail_table---------------------------//
    public synchronized long insertWeeklyDetailListData(String action, WeeklyDetailListData weeklyDetailListData) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.WeeklyDetailData.WEEKLY_ID, weeklyDetailListData.weeklyId);
            values.put(CloudContactsDB.WeeklyDetailData.WEEK_COUNT, weeklyDetailListData.weeklyCount);
            values.put(CloudContactsDB.WeeklyDetailData.YEAR, weeklyDetailListData.weeklyYear);
            values.put(CloudContactsDB.WeeklyDetailData.MONTH, weeklyDetailListData.weeklyMonth);
            values.put(CloudContactsDB.WeeklyDetailData.USER_ID, weeklyDetailListData.userId);
            values.put(CloudContactsDB.WeeklyDetailData.USER_NAME, weeklyDetailListData.userName);
            values.put(CloudContactsDB.WeeklyDetailData.USER_IMG, weeklyDetailListData.userImg);
            values.put(CloudContactsDB.WeeklyDetailData.CREATE_TIME, weeklyDetailListData.createTime);
            values.put(CloudContactsDB.WeeklyDetailData.CONTENT, weeklyDetailListData.content);
            values.put(CloudContactsDB.WeeklyDetailData.COMMENT, StringUtil.stringWithObject(weeklyDetailListData.comments));
            values.put(CloudContactsDB.WeeklyDetailData.GROUP_ID, weeklyDetailListData.groupId);
            values.put(CloudContactsDB.WeeklyDetailData.ACTION, action);
            flag = db.insert(CloudContactsDB.WeeklyDetailData.TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e(TAG, "insert_error" + e.toString());
        } finally {
            db.close();
        }
        Log.i(TAG, "--weekly_detail_table  insert--" + flag);
        return flag;
    }

    /**
     * 添加未读消息
     *
     * @param userId
     * @param msgDatas
     * @return
     */
    public synchronized long insertWeeklyUnreadListData(String userId, ArrayList<WeeklyMsgBean> msgDatas) {
        SQLiteDatabase db = null;
        long flag = 0l;
        for (int i = 0; i < msgDatas.size(); i++) {
            WeeklyMsgBean weeklyMsgBean = msgDatas.get(i);
            ContentValues values = new ContentValues();
            try {
                db = helper.getWritableDatabase();
                values.put(CloudContactsDB.WeeklyUnReadData.WEEKLY_ID, weeklyMsgBean.getWeekly_id());
                values.put(CloudContactsDB.WeeklyUnReadData.GROUP_ID, weeklyMsgBean.getGroup_id());
                values.put(CloudContactsDB.WeeklyUnReadData.USER_ID, userId);
                values.put(CloudContactsDB.WeeklyUnReadData.COMMENT_USER_ID, weeklyMsgBean.getComment_user_id());
                values.put(CloudContactsDB.WeeklyUnReadData.COMMENT_USER_IMAGE, weeklyMsgBean.getComment_user_image());
                values.put(CloudContactsDB.WeeklyUnReadData.COMMENT_USER_NAME, weeklyMsgBean.getComment_user_name());
                values.put(CloudContactsDB.WeeklyUnReadData.COMMENT_CONTENT, weeklyMsgBean.getComment_content());
                values.put(CloudContactsDB.WeeklyUnReadData.COMMENT_TIME, weeklyMsgBean.getComment_time());
                values.put(CloudContactsDB.WeeklyUnReadData.WEEKLY_TITLE, weeklyMsgBean.getWeekly_title());
                values.put(CloudContactsDB.WeeklyUnReadData.WEEKLY_CONTENT, weeklyMsgBean.getWeekly_content());
                flag = db.insert(CloudContactsDB.WeeklyUnReadData.TABLE_NAME, null, values);
            } catch (Exception e) {
                Log.e(TAG, "insert_error" + e.toString());
            } finally {
                db.close();
            }
            Log.i(TAG, "--weekly_unread_table  insert--" + flag);
        }
        return flag;
    }

    /**
     * @param type
     * @param year
     * @param month
     * @param action  my_weekly:我的周报  other:某人的周报 weekly_list:某周的所有周报
     * @param groupId
     * @return
     */
    public ArrayList<WeeklyDetailListData> findWeeklyDetailListData(int type, String year, String month, String action, String groupId, String weekCount) {
        ArrayList<WeeklyDetailListData> localData = new ArrayList<WeeklyDetailListData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            if ("my_weekly".equals(action)) {
                if (type == 0) {
                    cursor = db.query(CloudContactsDB.WeeklyDetailData.TABLE_NAME, null, "action = ? and group_id = ? ", new String[]{action, groupId}, null, null, null);
                } else if (type == 1) {
                    cursor = db.query(CloudContactsDB.WeeklyDetailData.TABLE_NAME, null, "action = ? and year = ? and group_id = ? ", new String[]{action, year, groupId}, null, null, null);

                } else if (type == 2) {
                    cursor = db.query(CloudContactsDB.WeeklyDetailData.TABLE_NAME, null, "action = ? and month = ? and group_id = ? ", new String[]{action, month, groupId}, null, null, null);

                } else if (type == 3) {
                    cursor = db.query(CloudContactsDB.WeeklyDetailData.TABLE_NAME, null, "action = ? and year = ? and month = ? and group_id = ? ", new String[]{action, year, month, groupId}, null, null, null);
                }

            } else if ("weekly_list".equals(action)) {
                cursor = db.query(CloudContactsDB.WeeklyDetailData.TABLE_NAME, null, " action = ? and year = ? and month = ? and week_count = ? and group_id = ? ", new String[]{action, year, month, weekCount, groupId}, null, null, null);

            }
            while (cursor.moveToNext()) {
                WeeklyDetailListData weeklyDetailListData = new WeeklyDetailListData();
                weeklyDetailListData.weeklyYear = cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.YEAR));
                weeklyDetailListData.weeklyMonth = cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.MONTH));
                weeklyDetailListData.weeklyCount = cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.WEEK_COUNT));
                weeklyDetailListData.weeklyId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.WEEKLY_ID));
                weeklyDetailListData.userId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.USER_ID));
                weeklyDetailListData.userName = cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.USER_NAME));
                weeklyDetailListData.userImg = cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.USER_IMG));
                weeklyDetailListData.createTime = cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.CREATE_TIME));
                weeklyDetailListData.content = cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.CONTENT));
                weeklyDetailListData.comments = (ArrayList<Map<String, String>>) StringUtil.objectWithString(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.COMMENT)));
                weeklyDetailListData.groupId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyDetailData.GROUP_ID));
                localData.add(weeklyDetailListData);
            }
        } catch (Exception e) {
            Log.e(TAG, "findWeeklyDetailData  error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return localData;
    }

    /**
     * 查询未读消息
     *
     * @param userId
     * @param groupId
     * @return
     */
    public ArrayList<WeeklyMsgBean> findWeeklyUnreadListData(String userId, String groupId) {
        ArrayList<WeeklyMsgBean> localData = new ArrayList<WeeklyMsgBean>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.WeeklyUnReadData.TABLE_NAME, null, " user_id = ? and group_id = ? ", new String[]{userId, groupId}, null, null, CloudContactsDB.WeeklyUnReadData.COMMENT_TIME + " desc");
            while (cursor.moveToNext()) {
                WeeklyMsgBean weeklyMsgBean = new WeeklyMsgBean();
                weeklyMsgBean.setWeekly_id(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyUnReadData.WEEKLY_ID)));
                weeklyMsgBean.setGroup_id(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyUnReadData.GROUP_ID)));
                weeklyMsgBean.setComment_user_id(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyUnReadData.COMMENT_USER_ID)));
                weeklyMsgBean.setComment_user_image(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyUnReadData.COMMENT_USER_IMAGE)));
                weeklyMsgBean.setComment_user_name(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyUnReadData.COMMENT_USER_NAME)));
                weeklyMsgBean.setComment_content(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyUnReadData.COMMENT_CONTENT)));
                weeklyMsgBean.setComment_time(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyUnReadData.COMMENT_TIME)));
                weeklyMsgBean.setWeekly_title(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyUnReadData.WEEKLY_TITLE)));
                weeklyMsgBean.setWeekly_content(cursor.getString(cursor.getColumnIndex(CloudContactsDB.WeeklyUnReadData.WEEKLY_CONTENT)));
                localData.add(weeklyMsgBean);
            }
        } catch (Exception e) {
            Log.e(TAG, " findWeeklyUnreadListData  error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return localData;
    }


    public int deleteAllWeeklyDetailList(String groupId, String action) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.WeeklyDetailData.TABLE_NAME, " group_id = ? and action = ? ", new String[]{groupId, action});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }

    public int deleteAllWeeklyDetailList() {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.WeeklyDetailData.TABLE_NAME, null, null);
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }


    public int deleteAllThisWeeklyDetailList(String groupId, String year, String month, String weekCount, String action) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.WeeklyDetailData.TABLE_NAME, " group_id = ? and action = ? and year = ? and month = ? and week_count = ? ", new String[]{groupId, action, year, month, weekCount});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }


    public int deleteAllWeeklyUnreadList(String groupId, String userId) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.WeeklyUnReadData.TABLE_NAME, " group_id = ? and user_id = ? ", new String[]{groupId, userId});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }

    public int deleteOneWeeklyUnreadList(String groupId, String weeklyId, String time, String userId) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.WeeklyUnReadData.TABLE_NAME, " group_id = ? and weekly_id = ? and comment_time = ? and user_id = ? ", new String[]{groupId, weeklyId, time, userId});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }


    //-----------------线索库筛选--------------------
    public synchronized long insertCRMClueFilter(int type,String parentId, String parentName, int parentSelected, CRMFilterSecondData secondData) {
        SQLiteDatabase db = null;
        long flag = 0l;
        ContentValues values = new ContentValues();
        try {
            db = helper.getWritableDatabase();
            values.put(CloudContactsDB.CRMClueLibraryFilterData.PARENT_ID, parentId);
            values.put(CloudContactsDB.CRMClueLibraryFilterData.PARENT_NAME, parentName);
            values.put(CloudContactsDB.CRMClueLibraryFilterData.PARENT_SELECTED, parentSelected);
            values.put(CloudContactsDB.CRMClueLibraryFilterData.CHILD_ID, secondData.getId());
            values.put(CloudContactsDB.CRMClueLibraryFilterData.CHILD_NAME, secondData.getName());
            values.put(CloudContactsDB.CRMClueLibraryFilterData.CHILD_SELECTED, secondData.getState());
            values.put(CloudContactsDB.CRMClueLibraryFilterData.IF_CHECKBOX,secondData.getType());
            values.put(CloudContactsDB.CRMClueLibraryFilterData.TYPE,type);
            flag = db.insert(CloudContactsDB.CRMClueLibraryFilterData.TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e(TAG, "insert_error" + e.toString());
        } finally {
            db.close();
        }
        Log.i(TAG, "--insertCRMClueLibraryFilterData--" + flag);
        return flag;
    }

    public ArrayList<CRMFilterFirstData> findCRMClueFilter(int type) {
        ArrayList<CRMFilterFirstData> filterFirstDatas = new ArrayList<CRMFilterFirstData>();
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = helper.getReadableDatabase();
            cursor = db.query(CloudContactsDB.CRMClueLibraryFilterData.TABLE_NAME, null, "type = ? ", new String[]{type+""}, null, null, null);
            String parentId = "";
            CRMFilterFirstData firstData = null;
            CRMFilterSecondData secondData = null;
            ArrayList<CRMFilterSecondData> secondDatas=null;
            while (cursor.moveToNext()) {
                if (parentId.equals(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.PARENT_ID)))) {
                    secondData = new CRMFilterSecondData();
                    secondData.setName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.CHILD_NAME)));
                    secondData.setId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.CHILD_ID)));
                    secondData.setState(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.CHILD_SELECTED)));
                    secondData.setType(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.IF_CHECKBOX)));
                    secondDatas.add(secondData);
                } else {
                    if(!parentId.equals("")){
                        firstData.setSecondDatas(secondDatas);
                        filterFirstDatas.add(firstData);
                    }
                    parentId = cursor.getString(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.PARENT_ID));
                    firstData = new CRMFilterFirstData();
                    firstData.setName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.PARENT_NAME)));
                    firstData.setId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.PARENT_ID)));
                    firstData.setState(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.PARENT_SELECTED)));
                    secondDatas = new ArrayList<CRMFilterSecondData>();
                    secondData = new CRMFilterSecondData();
                    secondData.setName(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.CHILD_NAME)));
                    secondData.setId(cursor.getString(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.CHILD_ID)));
                    secondData.setState(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.CHILD_SELECTED)));
                    secondData.setType(cursor.getInt(cursor.getColumnIndex(CloudContactsDB.CRMClueLibraryFilterData.IF_CHECKBOX)));
                    secondDatas.add(secondData);
                }

            }
            if(secondDatas!=null&&firstData!=null){
                firstData.setSecondDatas(secondDatas);
                filterFirstDatas.add(firstData);
            }
            Log.d(TAG, "   findCRMClueLibraryFilterData  " + filterFirstDatas.size());
        } catch (Exception e) {
            Log.e(TAG, "findCRMClueLibraryFilterData  error" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return filterFirstDatas;
    }

    public int deleteAllCRMClueFilter(int type) {
        SQLiteDatabase db = null;
        int delete = 0;
        try {
            db = helper.getWritableDatabase();
            delete = db.delete(CloudContactsDB.CRMClueLibraryFilterData.TABLE_NAME, "type = ? ", new String[]{type+""});
        } catch (SQLException e) {
            Log.i("delete", delete + "删除" + e);
        } finally {
            if (db != null) {
                db.close();
            }
        }
        Log.i("delete", delete + "删除");
        return delete;
    }

}
